1

嗨,我有一些 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];

但它没有插入任何东西。如果我通过 SQlitemanager 手动插入,那么我会收到错误,例如“Key1 ......

如果有人可以通过向我展示如何做到这一点来帮助我,我将不胜感激。

4

1 回答 1

3

您的插值字符串如下所示:

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

你需要 exacpe 字符串中的 " 因为它会混淆你的 rdbms。

也许试试

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

然而,真正的解决方案是在应用程序中使用准备好的语句而不是 SQL 字符串。当您使用准备好的语句时,这不是问题。

于 2012-11-28T17:25:51.353 回答