简体   繁体   English

如何从 android 中的 sqlite 中的 json 数据快速批量插入

[英]How to fast bulk insert from json data in sqlite in android

I have written this code to insert json data in to sqlite in android its working fine but i have 50000+ row to insert so its taking so much time to insert into sqlite database. I have written this code to insert json data in to sqlite in android its working fine but i have 50000+ row to insert so its taking so much time to insert into sqlite database. So how can I insert this data in fastest way please kindly give me the code I am very new in android.那么如何以最快的方式插入这些数据,请给我我在 android 中非常新的代码。 thank in advance.预先感谢。

Below i have written my code to insert data下面我编写了插入数据的代码

 private void insertItemDetails() {
        final ProgressDialog loading = ProgressDialog .show(this,"Updating Data From Tally","Please wait");

        StringRequest stringRequest=new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            loading.show();
                            itemDatabaseCon.open();
                            itemDatabaseCon.delete();
                            itemDatabaseCon.close();

                            itemDatabaseCon.open();
                            itemDatabaseCon.createTable();

                            int a=response.length();
//                            boolean b=a.equalsIgnoreCase("no");
                            Log.d("value", String.valueOf(a));

                            if (a==2) {
                                Log.d("inside item if loop ",response);
                            }
                            else {

                                JSONObject jsonObject = new JSONObject(response);
                                JSONArray array = jsonObject.getJSONArray("posts");
                                for (int i = 0; i < array.length(); i++) {
                                    JSONObject ob = array.getJSONObject(i);

                                    String stockid = ob.getString("stockid");
                                    String itemname = ob.getString("itemname");
                                    String group = ob.getString("group");
                                    String baseunit = ob.getString("baseunit");
                                    String alternateunit = ob.getString("alternateunit");
                                    String gst = ob.getString("gst");
                                    String hsn = ob.getString("hsn");
                                    String mrp = ob.getString("mrp");
                                    String sdtsellrate = ob.getString("sdtsellrate");
                                    String closingstock = ob.getString("closingstock");


                                    ContentValues contentValues = new ContentValues();
                                    contentValues.put(Constant2.key_itemstockid, stockid);
                                    contentValues.put(Constant2.key_itemname, itemname);
                                    contentValues.put(Constant2.key_itemgroup, group);
                                    contentValues.put(Constant2.key_itembaseunit, baseunit);
                                    contentValues.put(Constant2.key_itemalternateunit, alternateunit);
                                    contentValues.put(Constant2.key_itemgst, gst);
                                    contentValues.put(Constant2.key_itemhsn, hsn);
                                    contentValues.put(Constant2.key_itemmrp, mrp);
                                    contentValues.put(Constant2.key_itemsdtsellrate, sdtsellrate);
                                    contentValues.put(Constant2.key_itemclosingstock, closingstock);

                                    itemDatabaseCon.insert(Constant2.Table_name, contentValues);
                                }
                            }
                            loading.dismiss();

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("got api error ffff" , error.getMessage());

            }
        });
        RequestQueue requestQueue= Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);


    }

Here is my database controller code.这是我的数据库 controller 代码。

public class ItemDatabaseCon {


    String TAG = "DBAdapter";


    private SQLiteDatabase db;
    private ItemDatabaseCon.DBHelper dbHelper;

    public ItemDatabaseCon (Context context) {
        dbHelper = new ItemDatabaseCon.DBHelper(context);
    }

    public void open() {
        if (null == db || !db.isOpen()) {
            try {
                db = dbHelper.getWritableDatabase();
            } catch (SQLiteException sqLiteException) {
            }
        }
    }

    public void close() {
        if (db != null) {
            db.close();
        }
    }

    public int insert(String table, ContentValues values) {
        try {
            db = dbHelper.getWritableDatabase();
            int y = (int) db.insert(table, null, values);
            db.close();
            Log.e("Data Inserted", "Item Data Inserted");
            Log.e("number of row", y + "");
            return y;
        } catch (Exception ex) {
            Log.e("Error Insert", ex.getMessage().toString());
            return  0;
        }
    }

    public void delete() {
        db.execSQL("DROP TABLE IF EXISTS " + Constant2.Table_name);
    }

    public int getCount()
    {
        db = dbHelper.getWritableDatabase();
        String qry="SELECT * FROM "+Constant2.Table_name;
        Cursor cursor=db.rawQuery(qry,null);
        return cursor.getCount();


    }
    public void createTable()
    {
        String create_sql = "CREATE TABLE IF NOT EXISTS " + Constant2.Table_name + "("
                + Constant2.key_id + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + Constant2.key_itemstockid + " TEXT  ," + Constant2.key_itemname + " TEXT ," + Constant2.key_itemgroup + " TEXT ,"
                + Constant2.key_itembaseunit + " TEXT ,"+ Constant2.key_itemalternateunit + " TEXT ,"+ Constant2.key_itemgst + " TEXT ,"
                + Constant2.key_itemhsn + " TEXT ,"+ Constant2.key_itemmrp + " TEXT ,"+ Constant2.key_itemsdtsellrate + " TEXT ,"
                + Constant2.key_itemclosingstock + " TEXT " + ")";
        db.execSQL(create_sql);
    }

    public Cursor getAllRow(String table) {
        return db.query(table, null, null, null, null, null, Constant2.key_id);
    }

    private class DBHelper extends SQLiteOpenHelper {


        public DBHelper(Context context) {
            super(context, Constant2.DB_Name, null, Constant2.Db_Version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            String create_sql = "CREATE TABLE IF NOT EXISTS " + Constant2.Table_name + "("
                    + Constant2.key_id + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                    + Constant2.key_itemstockid + " TEXT  ," + Constant2.key_itemname + " TEXT ," + Constant2.key_itemgroup + " TEXT ,"
                    + Constant2.key_itembaseunit + " TEXT ,"+ Constant2.key_itemalternateunit + " TEXT ,"+ Constant2.key_itemgst + " TEXT ,"
                    + Constant2.key_itemhsn + " TEXT ,"+ Constant2.key_itemmrp + " TEXT ,"+ Constant2.key_itemsdtsellrate + " TEXT ,"
                    + Constant2.key_itemclosingstock + " TEXT " + ")";
            db.execSQL(create_sql);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + Constant2.Table_name);
        }

    }



   

}

You could do the inserts inside a single SQLite transaction.您可以在单个 SQLite 事务中进行插入。 This would significantly reduce the disk writes from 50000+ to very few.这将大大减少磁盘写入从 50000+ 到很少。

That is before the loops starts begin a transaction using the SQLiteDatabase's beginTransaction() method.那是在循环开始使用 SQLiteDatabase 的beginTransaction()方法开始事务之前。 After the loop has completed (all rows have been inserted) successfully use the setTransactionSuccessful() method followed by the endTransactionMethod()循环完成后(所有行都已插入)成功使用setTransactionSuccessful()方法,然后使用endTransactionMethod()

  • Note if you do not setTransactionSuccessful then the changes would be rolled back (so if you encounter an issue/error and want the changes (inserts) to not be applied use appropriate logic so that the setTransactionSuccessful is skipped but that the endTransaction is run)请注意,如果您没有setTransactionSuccessful ,那么更改将被回滚(因此,如果您遇到问题/错误并希望不应用更改(插入),请使用适当的逻辑,以便跳过setTransactionSuccessful但运行endTransaction

eg The following might be suitable:-例如以下可能是合适的:-

                    ....
                    else {
                        itemDatabaseCon.beginTransaction(); //<<<<<<<<<< ADDDED start the transaction
                        JSONObject jsonObject = new JSONObject(response);
                        JSONArray array = jsonObject.getJSONArray("posts");
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject ob = array.getJSONObject(i);

                            String stockid = ob.getString("stockid");
                            String itemname = ob.getString("itemname");
                            String group = ob.getString("group");
                            String baseunit = ob.getString("baseunit");
                            String alternateunit = ob.getString("alternateunit");
                            String gst = ob.getString("gst");
                            String hsn = ob.getString("hsn");
                            String mrp = ob.getString("mrp");
                            String sdtsellrate = ob.getString("sdtsellrate");
                            String closingstock = ob.getString("closingstock");


                            ContentValues contentValues = new ContentValues();
                            contentValues.put(Constant2.key_itemstockid, stockid);
                            contentValues.put(Constant2.key_itemname, itemname);
                            contentValues.put(Constant2.key_itemgroup, group);
                            contentValues.put(Constant2.key_itembaseunit, baseunit);
                            contentValues.put(Constant2.key_itemalternateunit, alternateunit);
                            contentValues.put(Constant2.key_itemgst, gst);
                            contentValues.put(Constant2.key_itemhsn, hsn);
                            contentValues.put(Constant2.key_itemmrp, mrp);
                            contentValues.put(Constant2.key_itemsdtsellrate, sdtsellrate);
                            contentValues.put(Constant2.key_itemclosingstock, closingstock);

                            itemDatabaseCon.insert(Constant2.Table_name, contentValues);
                        }
                        itemDatabaseCon.setTransactionSuccessful(); //<<<<<<<<<< ADDED indicate that changes (inserts) are all good
                        itemDatabaseCon.endTransaction(); //<<<<<<<<<< ADDED end the transaction
                    }
                    loading.dismiss();
                    ....
  • //<<<<<<<<<< indicates the changed/added code //<<<<<<<<<<表示更改/添加的代码

Edit编辑

However, considering the insert method the above will have no affect as you are closing the database after an insert.但是,考虑到插入方法,上述内容将没有影响,因为您在插入后关闭数据库。 Closing the database and then re-opening it is very costly resource wise.关闭数据库然后重新打开它是非常耗费资源的。

As such to benefit from running all the inserts in a single transaction you could use:-因此,为了受益于在单个事务中运行所有插入,您可以使用:-

public int insert(String table, ContentValues values) {
    try {
        db = dbHelper.getWritableDatabase();
        int y = (int) db.insert(table, null, values);
        //db.close(); //<<<<<<<<<< Commented out so as to not close the database
        Log.e("Data Inserted", "Item Data Inserted");
        Log.e("number of row", y + "");
        return y;
    } catch (Exception ex) {
        Log.e("Error Insert", ex.getMessage().toString());
        return  0;
    }
}

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

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