Android: bulk insert when InsertHelper is deprecated

There are many answers and tutorials that use InsertHelper for quick insertion into SQLiteDatabase. But InsertHelper is deprecated from API 17.

What is the fastest way to bulk input large datasets in Android SQLite?

So far, my biggest problem has been that SQLiteStatement is not very comfortable to work with, where InsertHelper has required columns and binding values, which was a bit trivial.

+17
android android-contentprovider sqlite bulkinsert android-sqlite
Jan 15 '13 at 18:15
source share
4 answers

SQLiteStatement also has binding methods, it extends SQLiteProgram .

Just run it in the transaction:

final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); final SQLiteStatement statement = db.compileStatement(INSERT_QUERY); db.beginTransaction(); try { for(MyBean bean : list){ statement.clearBindings(); statement.bindString(1, bean.getName()); // rest of bindings statement.execute(); //or executeInsert() if id is needed } db.setTransactionSuccessful(); } finally { db.endTransaction(); } 



EDIT

I cannot find a good solution in SQLiteQueryBuilder , but it is simple:

 final static String INSERT_QUERY = createInsert(DbSchema.TABLE_NAME, new String[]{DbSchema.NAME, DbSchema.TITLE, DbSchema.PHONE}); static public String createInsert(final String tableName, final String[] columnNames) { if (tableName == null || columnNames == null || columnNames.length == 0) { throw new IllegalArgumentException(); } final StringBuilder s = new StringBuilder(); s.append("INSERT INTO ").append(tableName).append(" ("); for (String column : columnNames) { s.append(column).append(" ,"); } int length = s.length(); s.delete(length - 2, length); s.append(") VALUES( "); for (int i = 0; i < columnNames.length; i++) { s.append(" ? ,"); } length = s.length(); s.delete(length - 2, length); s.append(")"); return s.toString(); } 
+26
Jan 15 '13 at 19:00
source share

I recommend that you take a look at the google IO application , especially provider , as Google developers do, so you can be sure that this is the right way.

+1
Jan 15 '13 at 18:29
source share

I ran into the same problem and couldn't find how SQLiteStatement can easily replace DatabaseUtils.InsertHelper, since you need to manually create the SQL query.

I ended up using SQLiteDatabase.insertOrThrow , which can easily replace existing code. I sometimes had to add an empty column to handle cases when I insert empty ContentValues.

Yes, the instruction was not compiled for reuse later, but creating an INSERT query manually and binding all parameters is too much pain. I would be interested to know how this effect affects a large bulk insert data set (InsertHelper is only deprecated) if you change your code.

+1
Jan 16 '13 at 17:12
source share

I just made a default copy of the class, keeping only the code associated with InsertHelper, check this meaning .

I am sure this is deprecated in order to clear the SDK utility classes. What remains with my class is the use of non-obsolete things.

+1
Apr 04 '13 at 13:18
source share



All Articles