2012-02-22 63 views
3

我需要运行一个查询,看起来会是什么样子FMDB查询与词典

INSERT INTO Appointments (field1, field2, field3, ..., field30) VALUES (value1, value2, value3, ..., value30) 

我已经被存储在一个字典里我的任命,并希望通过该字典使钥匙圈等于字段和值等于这些值。

我试图使用executeUpdate:... withParameterDictionary:...但无法弄清楚如何使多个领域的工作,如果我不知道的字段名。字段名称正在通过JSON发送,而不是手工输入了30场我只是想通过字典环,并得到他们的方式。

我甚至试过

NSMutableArray *keys = nil; 
NSMutableArray *values = nil; 

     for (NSDictionary *dict in [json objectForKey:@"data"]) { 
      keys = [NSMutableArray array]; 
      values = [NSMutableArray array]; 
      for (id key in dict) { 
       [keys addObject:key]; 
       [values addObject:[NSString stringWithFormat:@":%@", key]]; 
      } 
      NSString *keyString = [keys componentsJoinedByString:@","]; 
      NSString *valueString = [values componentsJoinedByString:@","]; 
      [[dataObj db] executeUpdate:@"DELETE FROM Appointments"]; 
      NSLog(@"INSERT INTO Appointments (%@) VALUES (%@)", keyString, valueString); 
      [[dataObj db] executeUpdate:@"INSERT INTO Appointments (?) VALUES (?)", keyString, valueString]; 

     } 

上面的代码打印的NSLog如何查询应长相,但没有被插入到数据库中。我知道这是因为我在查询运行后打开模拟器数据库文件,它仍然是空白的。

我怎样才能得到上面的代码工作或我如何能得到executeQuery:... withParameterDictionary:...与多个名称的工作。

回答

6

我跑了几个快速测试,这对我的作品:

NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:@"AAAA44", @"a", @"BBBB44", @"b", @"CCCC44", @"c", nil]; 
NSMutableArray* cols = [[NSMutableArray alloc] init]; 
NSMutableArray* vals = [[NSMutableArray alloc] init]; 
for (id key in dict) { 
    [cols addObject:key]; 
    [vals addObject:[dict objectForKey:key]]; 
} 
NSMutableArray* newCols = [[NSMutableArray alloc] init]; 
NSMutableArray* newVals = [[NSMutableArray alloc] init]; 
for (int i = 0; i<[cols count]; i++) { 
    [newCols addObject:[NSString stringWithFormat:@"'%@'", [cols objectAtIndex:i]]]; 
    [newVals addObject:[NSString stringWithFormat:@"'%@'", [vals objectAtIndex:i]]]; 
} 
NSString* sql = [NSString stringWithFormat:@"insert into test (%@) values (%@)", [newCols componentsJoinedByString:@", "], [newVals componentsJoinedByString:@", "]]; 
NSLog(@"%@", sql); 
BOOL updateSuccess = [db executeUpdate:sql]; 

诀窍是添加'到阵列中的数据。

+0

你是对的,我昨天做了这个,并得到它的工作,但忘了发布。我会测试你发布的字典内容。 – Bot 2012-02-23 19:06:51

+3

这太好了。唯一的缺点是你跳过FMDB中的所有内置转义。这可能会被注入取决于你如何获得所有参数。 – Julian 2012-08-09 06:20:05

+0

我碰到了类似的问题,无法弄清楚如何使用的executeQuery withParameterDictionary和自动递增的关键。 这里介绍的方法将工作,但绕过内置逃脱,这是一个令人失望:( – selytch 2013-02-23 10:00:38

2
NSDictionary *argsDict 
    = [NSDictionary dictionaryWithObjectsAndKeys:@"My Name", 
     @"name", nil]; 

[db executeUpdate:@"INSERT INTO myTable (name) VALUES (:name)" 
    withParameterDictionary:argsDict]; 
0

以下是我刚刚写入的一些示例代码,以便在插入时支持可选值。只是简单的测试,但我认为它的作品。

NSMutableDictionary* fieldsandvalues = [NSMutableDictionary dictionary]; 
    fieldsandvalues[@"word"] = userphrase.word; 
    fieldsandvalues[@"translation"] = userphrase.translation; 
    if (userphrase.samplesentence.length > 0) { 
     fieldsandvalues[@"samplesentence"] = userphrase.samplesentence; 
    } 
    if (userphrase.notes.length > 0) { 
     fieldsandvalues[@"notes"] = userphrase.notes; 
    } 

    NSMutableArray* keyswithcolon = [NSMutableArray array]; 
    for (NSString* key in fieldsandvalues.allKeys) { 
     [keyswithcolon addObject:[NSString stringWithFormat:@":%@", key]]; 
    } 

    NSString* sql = [NSString stringWithFormat:@"INSERT INTO userphrase (%@) VALUES (%@)", [fieldsandvalues.allKeys componentsJoinedByString:@","], [keyswithcolon componentsJoinedByString:@","]]; 
// DLog(@"sql: %@", sql); 
    if (![self.db executeUpdate:sql withParameterDictionary:fieldsandvalues]) { 
     NSAssert(NO, @"Failed inserting userphrase into database! Last error: %@ - %@", self.db.lastError, self.db.lastErrorMessage); 
     return nil; 
    }