gpt4 book ai didi

iphone - 如何在iOS中将JSON字符串保存到Sqlite的一个字段中?

转载 作者:行者123 更新时间:2023-12-03 18:38:14 25 4
gpt4 key购买 nike

嗨,我有一些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 ......错误”之类的错误。

如果有人可以向我展示如何帮助我,我将非常感激。

最佳答案

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

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

您需要排除字符串中的“”,因为它会使您的rdbms混乱。

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

但是,真正的解决方案是在应用程序中使用Prepared Statements而不是SQL String。使用准备好的语句时,这不是问题。

关于iphone - 如何在iOS中将JSON字符串保存到Sqlite的一个字段中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13610812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com