【发布时间】:2023-03-02 23:28:01
【问题描述】:
如何将来自服务器的批量 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 数组,它本身就是嵌套的。有没有更好的方法可以在很短的时间内快速插入大量数据?
【问题讨论】: