我有一个 NSDictionary 包含解析的 JSON 数据。如何将这些数据推送到数据库,以便从数据库中提取它们以供进一步使用?
我正在使用 SBJSON 进行解析。
我有一个 NSDictionary 包含解析的 JSON 数据。如何将这些数据推送到数据库,以便从数据库中提取它们以供进一步使用?
我正在使用 SBJSON 进行解析。
如果您的设计要求指定 sqlite,那么我建议您使用 Gus Mueller 的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];
也就是说,正如 Chris 上面所说,Core Data 通常是比 sqlite 更好的解决方案。Brent Simmons(NetNewsWire 开发人员)有一系列关于这个主题的帖子,比如这个。
对我来说,“Core Data 比 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>0?@"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];
在这件事上使用核心数据。这是一个很好的教程:http: //mobile.tutsplus.com/tutorials/iphone/iphone-core-data/