gpt4 book ai didi

android - 如何在 Android 中从 Json 批量插入到 Sqlite

转载 作者:IT王子 更新时间:2023-10-29 06:26:55 26 4
gpt4 key购买 nike

<分区>

如何非常高效地将来自服务器的批量 json 数据插入到 Android 中的 Sqlite 数据库中。我现在使用的方法效率很低,插入大约2000条记录需要将近一分钟的时间。我遵循的方法是:

   for (int i = 0; i < jsonObj.length(); i++) 
{
JSONObject itemObj = (JSONObject) jsonObj.get(i);

ContentValues value1 = new ContentValues();
ContentValues value2 = new ContentValues();

value2.put(DbHelper.BusinessID,itemObj.getString("BusinessID"));
value2.put(DbHelper.LocationID,itemObj.getString("LocationID"));
JSONArray list = itemObj.getJSONArray("OfficeDetails");

if (list != null)
{
for (int k = 0; k < list.length(); k++)
{
JSONObject elem = list.getJSONObject(k);
if (elem != null)
{
try
{
value1.put(DbHelper.Office_Code,elem.getInt("Office_Code"));
value1.put(DbHelper.Office_District,elem.getInt("Office_District"));

db.insert(DbHelper.MessageDetail,null, value1);
}
catch (Exception e)
{
e.printStackTrace();
}
}
db.insert(DbHelper.Message,null, value2);
}

即将到来的输入是一个嵌套的 Json 数组,它本身是嵌套的。有没有更好的方法可以在很短的时间内快速插入大量数据?

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