Usually, every time db.insert() , SQLite creates a transaction (and the resulting log file in the file system), which slows down.
If you use db.beginTransaction() and db.endTransaction() , SQLite creates only one log file in the file system and then commits all the inserts at a time, greatly speeding up the work.
Here are a few pseudo codes: Batch insert into SQLite database on Android
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.endTransaction(); }
If you want to abort a transaction due to an unexpected error or something else, just db.endTransaction() without first setting up the transaction ( db.setTransactionSuccessful() ).
Another useful method is to use db.inTransaction() (returns true or false ) to determine if you are currently in the middle of a transaction.
Documentation here
Jake Wilson Sep 21 '11 at 17:36 2011-09-21 17:36
source share