8

I've got an activity that handles the printers of my University. The Printers can be downloaded over the internet and are then stored in a SQLite database. The problem is, that there are about 500 database entries that have to be made to store them, which is with my code very time intensive (approx. 30s on a Google Nexus S). My code to do so is this:

printerDB = new PrinterListOpenHelper(this);
SQLiteDatabase db = printerDB.getWritableDatabase();
db.execSQL("INSERT INTO destination (id, destination) VALUES(1,'BSAC240');");
db.execSQL("INSERT INTO destination (id, destination) VALUES(2,'BSAD152');");
...

This is followed by approx. 500 similar rows. I also tried to do it with a single

db.rawQuerry("INSERT INTO destination (id, destination) VALUES(1,'BSAC240');
INSERT INTO destination (id, destination) VALUES(2,'BSAD152');.......");

but then only the first INSERT statement is actually executed.

Does anybody know a trick for making this efficient? Or are the Android Databases actually that slow?

Thanks a lot for your help! Simon

1 Answer 1

17

Does it work any better if you put them into a transaction?

BEGIN TRANSACTION
  INSERT INTO destination (id, destination) VALUES(1,'BSAC240');
  INSERT INTO destination (id, destination) VALUES(2,'BSAD152');
END TRANSACTION

Or even (updated following comments)

db.beginTransaction()
try {
    db.execSQL("INSERT INTO destination (id, destination) VALUES(1,'BSAC240');");
    db.execSQL("INSERT INTO destination (id, destination) VALUES(2,'BSAD152');");
    db.setTransactionSuccessful();
} finally {
    db.endTransaction();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Well spotted, and obviously wrap it with a try/catch as well
Thx! That was exactly what I was looking for, helped me a lot! Do you know why we technically have to do it this way?
Wrapping it in a transaction means they are all done together exclusively. Without that each one is done in its own.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.