Using DatabaseUtils.InsertHelper for faster insertions into SQLite database

http://www.outofwhatbox.com/blog/2010/12/android-using-databaseutils-inserthelper-for-faster-insertions-into-sqlite-database/

Thisisagoodarticle.

twowaystodobatchinsert:

try{
  db.beginTransaction();
  for each record in the list {
     do_some_processing();
     if (line represent a valid  entry) {
        db.insert(SOME_TABLE, null, SOME_VALUE);
     }
     some_other_processing();
  }
  db.setTransactionSuccessful();
} catch (SQLException e) {
} finally {
  db.endTranscation();
}
try {
    mDb.beginTransaction();
    InsertHelper ih = new InsertHelper(db, "columnTable");
    for (Value value : values) {
        ih.prepareForInsert();
        
        ih.bind(colIdx, value.getSomeValue());
        // ...
        ih.execute();
    }
    mDb.setTransactionSuccessful();
} finally {
    mDb.endTransaction();           
}

Thesecondoneisreallyhelpful,becauseiwanttoinserttherecordswiththefieldindexandwithoutknowingthefieldname.

相关推荐