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:...
处理多个名称。
I need to run a query that looks would look like
INSERT INTO Appointments (field1, field2, field3, ..., field30) VALUES (value1, value2, value3, ..., value30)
I have my Appointments being stored inside a Dictionary and would like to loop through that dictionary to make the keys equal the fields and the values equal the values.
I'm trying to use the executeUpdate:... withParameterDictionary:...
but can't figure out how to make that work with multiple fields if I don't know the field names. The field names are being sent via JSON and instead of manually typing out 30 fields I would just like to loop through the dictionary and get them that way.
I have even tried
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];
}
The code above prints the NSLog how the query should looks but nothing is being inserted into the database. I know this because I am opening the simulator database file after the queries run and it is still blank.
How can I get the above code to work or how can I get the executeQuery:... withParameterDictionary:...
to work with multiple names.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我进行了一些快速测试,这对我有用:
技巧是将
'
添加到数组中的数据中。I ran a couple of quick tests, and this works for me:
The trick is to add
'
to the data in the arrays.这是我刚刚编写的一些示例代码,用于在插入时支持可选值。只是简单测试过,但我认为它有效。
Here is some sample code I just wrote to support optional values at insert time. Just briefly tested but I think it works.