我们的app使用FMDatabase操作sqlite3,其中有段代码:
NSString *sql = @"update tb_users set baseInfo_name = :baseInfo_name, contact_email = :contact_email, baseInfo_image = :baseInfo_image; NSDictionary *data = [datas objectAtIndex:0]; BOOL result = [db executeUpdate:sql withParameterDictionary:mutable];
查看了一下FMDatabase的源码:
// If dictionaryArgs is passed in, that means we are using sqlite's named parameter support if (dictionaryArgs) { for (NSString *dictionaryKey in [dictionaryArgs allKeys]) { // Prefix the key with a colon. NSString *parameterName = [[NSString alloc] initWithFormat:@":%@", dictionaryKey]; // Get the index for the parameter name. int namedIdx = sqlite3_bind_parameter_index(pStmt, [parameterName UTF8String]); FMDBRelease(parameterName); if (namedIdx > 0) { // Standard binding from here. [self bindObject:[dictionaryArgs objectForKey:dictionaryKey] toColumn:namedIdx inStatement:pStmt]; // increment the binding count, so our check below works out idx++; } else { NSLog(@"Could not find index for %@", dictionaryKey); } } }