将SQLite3数据库转换为JSON iOS [英] Convert SQLite3 Database to JSON iOS

查看:170
本文介绍了将SQLite3数据库转换为JSON iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索过谷歌的教程以帮助解决这个问题,但却找不到任何全面的内容。

I have scoured Google for a tutorial to help with this but haven't been able to find anything comprehensive.

我想单向同步一个SQLite3数据库使用Web服务通过以JSON格式发送数据库中包含的数据,但是无法找到有关如何将数据库转换为JSON的信息。如果有人可以指向我的教程方向,或者显示如何转换简单的SQLite表的简短示例,那就太棒了!

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.

谢谢!

推荐答案

答案很简单:你必须从SQL数据库中获取所有信息到一些抽象数据类型,然后序列化那些数据进入JSON。我假设您正在使用Objective-C进行开发,因为它是iOS编程的主要语言。我已经为两个目的编写了库,所以使用我的 SQL助手类和我的 JSON库,您可以这样做:

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

我希望这有帮助。

这篇关于将SQLite3数据库转换为JSON iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆