可能重复:
separating keys and objects from NSMutable dictionary and use the values in insert command of sqlite如何将数据从NSDictionary写入SQLite数据库?
我有一个包含已解析JSON数据一个NSDictionary。如何将这些数据推送到数据库,以便我可以从数据库中提取它们以供进一步使用?
我正在使用SBJSON进行解析。
可能重复:
separating keys and objects from NSMutable dictionary and use the values in insert command of sqlite如何将数据从NSDictionary写入SQLite数据库?
我有一个包含已解析JSON数据一个NSDictionary。如何将这些数据推送到数据库,以便我可以从数据库中提取它们以供进一步使用?
我正在使用SBJSON进行解析。
对此事使用核心数据。这里是一个很好的教程入手: http://mobile.tutsplus.com/tutorials/iphone/iphone-core-data/
如果您的设计要求规定sqlite的,那么我会建议使用格斯穆勒的FMDB让你不必直接与原始SQLite的工作。
NSString $title = [jsonDictionary objectForKey:@"title"];
// other keys, values, etc...
NSString $query = [NSString stringWithFormat:@"INSERT INTO myTable t (t.some_column) VALUES ('%@'),$title];
FMResultSet *results = [_db executeQuery:$query];
这就是说,正如克里斯在上面说的,Core Data通常是比sqlite更好的解决方案。布伦特西蒙斯(NetNewsWire开发人员)有一系列关于此主题的帖子,如this one.
对于“核心数据比sqlite更好”的例外情况是,我想要提供初始数据但不想执行到Core Data的初始导入。
以下代码将创建动态plist文件,并将该文件存储到捆绑包中,并且可以访问数据并将数据插入到.plist文件中。
NSString *strPathToAudioCache=[NSString stringWithFormat:@"%@/%@",
[(NSArray*)NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0],
@"data.plist"];
NSMutableDictionary *dOfAudios=[NSMutableDictionary dictionaryWithContentsOfFile:strPathToAudioCache];
NSLog([dOfAudios allKeys].count>[email protected]"Value is exist":@"Value is not exist");
if([dOfAudios allKeys].count>0) {
[dOfAudios setValue:@"Key_for_value" forKey:@"value_for_key"];
} else {
dOfAudios=[NSMutableDictionary dictionary];
[dOfAudios setValue:@"Key_for_value" forKey:@"value_for_key"];
}
[dOfAudios writeToFile:strPathToAudioCache atomically:YES];
检查这个问题,并检查出accpeted答案的代码,我认为它可以帮助你http://stackoverflow.com/questions/5677396/separating-keys-and-objects-from-nsmutable-dictionary-and -use-the-values-in-inser – Ballu