【问题标题】:How to bulk insert from Json to Sqlite in Android [duplicate]如何在Android中从Json批量插入到Sqlite [重复]
【发布时间】: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 数组,它本身就是嵌套的。有没有更好的方法可以在很短的时间内快速插入大量数据?

【问题讨论】:

    标签: android sql json sqlite


    【解决方案1】:

    你可以试试bulkInsert如下:

    ContentResolver.bulkInsert (Uri url, ContentValues[] values); //Array of rows to be inserted
    

    这仅适用于仅使用 1 个 URI 的情况,如果要使用多个 uri,则应在 ContentResolver 中使用 applyBatch 方法。

    希望对你有帮助

    【讨论】:

    • 你能举个例子吗?什么是 Uri?
    【解决方案2】:

    首先创建 json 数据的模型类。使用 Gson,您可以在数组列表中获取数据。然后您可以使用该数组列表将数据插入到 sqlite。 GreenDao 是您获得快速性能的最佳选择。

    当您在流中接收 json 数据时,请使用以下代码:

    Type collectionType = new TypeToken<ArrayList<your_model>>() {}.getType();
    Gson gson = new Gson();
    ArrayList<your_model> yourModelList = gson.fromJson(stream, collectionType);
    

    【讨论】:

    • 一旦我创建了arraylist,我将如何将它插入到数据库中?我应该使用我已经使用过的方法吗?
    • 您需要使用数据库助手。 ORMlite 非常易于使用。 GreenDAO 是另一种选择。使用greendao很容易。但你已经读过它。一旦你得到它。它要快得多。
    【解决方案3】:

    Github 中有一个名为 JSQL 的优秀库。

    https://github.com/wenchaojiang/JSQL

    它使得将持久 JSON 字符串和 JSON 对象保存到数据库上的 SQLite 变得如此容易。

    【讨论】:

      【解决方案4】:

      创建您的 json 数据列表,然后使用此自定义查询进行批量数据插入:

      /**
       * insert the bulk data into database
       * 
       * @param query to insert data into table
       * @param parameter data to be inserted into table
       * @return number of rows got inserted
       */
      protected int insertBulk(String query, String[][] parameter) throws SQLiteConstraintException, SQLiteException {
          int rowCount = -1;
          SQLiteStatement statement = mSqldb.compileStatement(query);
          mSqldb.beginTransaction();
          try {
              for (int index = 0; index < parameter.length; index++) {
                  statement.bindAllArgsAsStrings(parameter[index]);
                  statement.execute();
                  statement.clearBindings();
              }
              rowCount = parameter.length;
          } finally {
              mSqldb.setTransactionSuccessful();
              mSqldb.endTransaction();
          }
          return rowCount;
      }
      

      【讨论】:

      • 事务处理不正确;发生错误时,不能调用setTransactionSuccessful()。
      • 感谢@cl。我会检查这个:)
      猜你喜欢
      • 1970-01-01
      • 2017-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-12
      • 2015-11-23
      • 1970-01-01
      • 2011-01-27
      相关资源
      最近更新 更多