SQLite inserts wrapped in one transaction: no speed improvement
I read many times that in order to improve SQLite Inserts operations, they had to be wrapped into one transaction. This is what I did in my case: I have 3 lists of "String", "long" and "int". I'm opening the database and compiling the statement using this code:
mDb = mDataDB.getWritableDatabase();
String INSERT_QUERY = "INSERT INTO "+ mDataDB.TABLE_SENSORS_DATA + " (" +
mDataDB.COLUMN_SENSOR_ID +", " + mDataDB.COLUMN_TIME_STAMP + ", " +
mDataDB.COLUMN_PRESSURE + ")" +" VALUES(?,?,?);";
statement = mDb.compileStatement(INSERT_QUERY);
Then I start the transaction:
mDb.beginTransaction();
And I use a loop to write add the data from the lists:
Iterator timeCursor = timeStamps.iterator();
Iterator dataCursor = data.iterator();
for(int i =0; i < dataCount; i++){
CurrentSensor.dataDAO.addData(sensor_id, (long)timeCursor.next(), (int) dataCursor.next());
Log.i("data iteration " + i,"data added to the db");
}
The code of addData is:
try {
statement.clearBindings();
statement.bindString(1, sensor_id);
statement.bindLong(2, time_stamp);
statement.bindLong(3, (long) pressure);// I can't bind integer ?!
statement.execute();
}catch(Exception e) {
e.printStackTrace();
}
And I finally close the transaction. Still, the time taken to insert is really long (around 10ms for each row, and I have thousands of them...). The thing is I was using the basic "insert" function of SQlite before, and the duration was the same. Would anyone have a suggestion?
链接地址: http://www.djcxy.com/p/19824.html上一篇: SQLite插入非常慢?