简体   繁体   English

Objective-C:将JSON响应添加到SQLite DB

[英]Objective-C: Add JSON response to SQLite DB

I am getting a JSON response and would like to add it to an SQLite db. 我收到JSON响应,并想将其添加到SQLite数据库中。 The results come back as an array and in each array there will be about 30 keys with values. 结果作为一个数组返回,每个数组中大约有30个带有值的键。 What would be the most efficient approach to adding all those to my table? 将所有这些添加到我的表中最有效的方法是什么? The keys coming from the JSON would be the columns in the sqlite db. 来自JSON的键将是sqlite db中的列。

Would it be best to do a for loop on each array item then another for loop in side to get the values and add it to a string then add them to the database that way? 最好在每个数组项上执行一个for循环,然后在另一端进行另一个for循环以获取值并将其添加到字符串,然后以这种方式将其添加到数据库中吗? Or is there a better approach using FMDB to add a JSON response directly to the database if the json keys match the database table columns? 还是有更好的方法使用FMDB将JSON响应直接添加到数据库(如果JSON键与数据库表列匹配)?

If you believe your JSON response will not change and your data model will not change (or rarely change), then I'd just loop through the arrays and write the slightly long... 如果您认为您的JSON响应不会改变并且您的数据模型不会改变(或很少改变),那么我将遍历数组并编写稍长的时间...

[db executeUpdate:@"INSERT INTO response (key1,key2,..key30) VALUES (?,?,?...etc);", json_reponse.value1,json_response.value2,...,json_response.value30, nil];

However, if this model would change, be extended, etc... then I'd probably just use Core Data. 但是,如果此模型将更改,扩展等...那么我可能只会使用Core Data。

The biggest factor though is what are you doing with the data after it is stored? 不过,最大的因素是数据存储后您将如何处理? Creating objects, display a report, converting the objects back to JSON (then just store the raw JSON in a text field)? 创建对象,显示报告,将对象转换回JSON(然后将原始JSON存储在文本字段中)?

我最后做了一个循环,得到了json键并将其用作列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM