您好我有一些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 ......
如果有人能通过向我展示如何做到这一点,我将非常感激。
答案 0 :(得分:3)
您的interporlated字符串将如下所示:
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];
然而,真正的解决方案是在应用程序中使用Prepared Statements而不是SQL String。使用预准备语句时,这不是问题。