如何将数据从 NSDictionary 写入 SQLite 数据库?

发布于 2024-12-09 03:58:44 字数 351 浏览 0 评论 0原文

可能的重复:
从 NSMutable 中分离键和对象字典并使用sqlite的插入命令中的值

我有一个包含解析的 JSON 数据的 NSDictionary。如何将这些数据推送到数据库,以便我可以从数据库中提取它们以供进一步使用?

我正在使用 SBJSON 进行解析。

Possible Duplicate:
separating keys and objects from NSMutable dictionary and use the values in insert command of sqlite

I have an NSDictionary containing parsed JSON data. How can I push these data to a database so that I can extract them from database for further use?

I am using SBJSON for parsing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恍梦境° 2024-12-16 03:58:45

如果您的设计要求指定了 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 的初始导入的情况。

If your design requirements specify sqlite, then I would recommend using Gus Mueller's FMDB so that you do not have to work directly with raw 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];

That said, as Chris said above, Core Data is often a better solution than sqlite. Brent Simmons (NetNewsWire developer) has a series of posts about this subject, like this one.

The exception to the "Core Data is better than sqlite" mantra for me is the situation where you want to provide initial data but do not want to perform an initial import into Core Data.

撩动你心 2024-12-16 03:58:45

使用核心数据来解决这个问题。这是一个很好的入门教程:
http://mobile.tutsplus.com/tutorials/iphone/iphone-core-数据/

Use core data for this matter. Here is a good tutorial to start with:
http://mobile.tutsplus.com/tutorials/iphone/iphone-core-data/

仙气飘飘 2024-12-16 03:58:45

以下代码用于创建动态 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];

Following code goes to create the dynamic plist file and that file stores into the bundle and that data can be access and the insert the data into the .plist file.

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文