android sqlite 批量插入数据优化代码
SQLiteDatabase db = dh.getWritableDatabase();db.execSQL("PRAGMA cache_size=12000;");db.beginTransaction();try {for (int i = 0; i < 10000; i++) {String id = "8aebf7b03e410db4013e43ed2369" + String.format("%04d", i);String sql = generateSql();SQLiteStatement sqlListStatment = db.compileStatement(sql);sqlListStatment.bindString(1, id);sqlListStatment.bindString(2, "20130426091902-187");sqlListStatment.executeInsert();// db.execSQL(sql);}db.setTransactionSuccessful();} catch (android.database.SQLException e) {e.printStackTrace();} finally {db.endTransaction();db.close();dh.close();}
try {TransactionManager.callInTransaction(em.getConnectionSource(), new Callable<Void>() {@Overridepublic Void call() throws Exception {try {Dao<?, ?> dao = null;dao = em.findDao(Task.class);em.enableCache();for (int i = 0; i < 10000; i++) {String id = "8aebf7b03e410db4013e43ed2369" + String.format("%04d", i);String sql = generateSql();Log.d("MyTag", sql);String[] args = new String[] { id, "20130426091902-187" };dao.executeRaw(sql, args);}} catch (SQLException e) {e.printStackTrace();}return null;}});} catch (SQLException e) {e.printStackTrace();}