Bulk Insertion on Android device

后端 未结 5 813
刺人心
刺人心 2020-11-28 02:40

I want to bulk insert about 700 records into the Android database on my next upgrade. What\'s the most efficient way to do this? From various posts, I know that if I use <

5条回答
  •  暖寄归人
    2020-11-28 03:07

    This example below will work perfectly

     String sql = "INSERT INTO " + DatabaseHelper.TABLE_PRODUCT_LIST
                    + " VALUES (?,?,?,?,?,?,?,?,?);";
    
            SQLiteDatabase db = this.getWritableDatabase();
            SQLiteStatement statement = db.compileStatement(sql);
            db.beginTransaction();
            for(int idx=0; idx < Produc_List.size(); idx++) {
                statement.clearBindings();
                statement.bindLong(1, Produc_List.get(idx).getProduct_id());
                statement.bindLong(2,  Produc_List.get(idx).getCategory_id());
                statement.bindString(3, Produc_List.get(idx).getName());
    //            statement.bindString(4, Produc_List.get(idx).getBrand());
                statement.bindString(5, Produc_List.get(idx).getPrice());
                //statement.bindString(6, Produc_List.get(idx).getDiscPrice());
                statement.bindString(7, Produc_List.get(idx).getImage());
                statement.bindLong(8, Produc_List.get(idx).getLanguage_id());
                statement.bindLong(9, Produc_List.get(idx).getPl_rank());
                statement.execute();
    
            }
            db.setTransactionSuccessful();
            db.endTransaction();
    

提交回复
热议问题