I have scoured Google for a tutorial to help with this but haven't been able to find anything comprehensive.
I want to one-way sync an SQLite3 database with a web service by sending the data contained in the database in JSON format but am having trouble finding information about how to convert the database into JSON. If anyone can either point me in the direction of a tutorial covering this or alternatively show a brief example of how to go about converting a simple SQLite table, that would be great!
The database contains about ten tables with the potential to contain a lot of data so aside from a simple example, some general guidance would be appreciated.
Thanks!
The answer is quite simple: you have to get all the information out of the SQL database into some abstract data type, then serialize that data into JSON. I assume that you're developing in Objective-C since it's the principal language for iOS programming. I've written libraries for both purposes, so using my SQL helper class and my JSON library, you can do something like this:
SQLHelper *db = [[SQLHelper alloc] initWithFile:@"/User/Library/large_db.sqlite3"]; // specify full file path
// repeat these 3 steps for every table
NSArray *allContents = [db executeQuery:@"SELECT * FROM first_table"];
NSString *json = [allContents generateJson];
// now send `json' to the web service
// cleanup once we finished
[db release];
I hope this helps.
I have made an ios app, and tried these two library -
- db library - https://github.com/ccgus/fmdb
- json library - https://github.com/stig/json-framework
I think it's easy to make a json text from db, first read all data to NSDictionary and then convert it to json text. You can check these two library.