开发者

How can I write data from an NSDictionary to a SQLite database? [duplicate]

开发者 https://www.devze.com 2023-04-12 02:51 出处:网络
This question already has answers here开发者_Go百科: Closed 11 years ago. Possible Duplicate: separating keys and objects from NSMutable dictionary and use the values in insert command of sqli
This question already has answers here开发者_Go百科: Closed 11 years ago.

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.


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.


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


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];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号