2 starwar2012 starwar2012 于 2012.11.29 10:58 提问

保存JSON字符串到sqlite中

数据是JSON格式的,这样:


contentJson=[{"key1":"Value1","key2":"Value2","key3":"Value3","key4":"Value4","key5":"Value5","key6":"Value6","key7":"Value7", "key8":"Value8"},{"key1":"Value1","key2":"Value2","key3":"Value3","key4":"Value4","key5":"Value5","key6":"Value6","key7":"Value7", "key8":"Value8"}]


怎么样把这些数据保存到Sqlite数据库中?

实现代码:

NSString *statement = [[NSString alloc]initWithFormat:@"INSERT INTO mydatabase(contentJson,ID) VALUES(\"%@\",%d)",contentJson,ID];

但是没有成功,非常感谢帮忙。

1个回答

g989_1314125
g989_1314125   2012.11.29 13:37

你插入的数据应该是这样:

INSERT INTO mydatabase(contentJson,ID) VALUES(
"[{"key1":"Value1","key2":"Value2","key3":"Value3","key4":"Value4",
"key5":"Value5","key6":"Value6","key7":"Value7", "key8":"Value8"},
{"key1":"Value1","key2":"Value2","key3":"Value3","key4":"Value4",
"key5":"Value5","key6":"Value6","key7":"Value7", "key8":"Value8"}]",
ID);

不能使用这个符号",它影响数据库。
用这行代码:

NSString *statement = [[NSString alloc]initWithFormat:@"INSERT INTO mydatabase(contentJson,ID) VALUES('%@',%d)",contentJson,ID];
Csdn user default icon
上传中...
上传图片
插入图片