4

I have around 7000 rows which want to insert into device's(Android galaxy tab2) sqlite database. Currently I am parsing the JSON file and inserting the row into the database using "for" loop. But its taking more than 120 seconds to parse and inset data into sqlite database. Is there any other faster way to do this?

Thanks...

6
  • 3
    Use database transactions. Commented Apr 18, 2013 at 17:50
  • 1
    can you insert all data with one query? or by batch, like 500 per batch? Commented Apr 18, 2013 at 17:52
  • Another idea would be pre-insert to a sqlite data, and host the database file somewhere, and download the file and open it directly. That is rather dramatic changes. Commented Apr 18, 2013 at 17:53
  • @dongshengcn : good idea! but i think, i have to create a .sqlite file first. Can i generate a .sqlite file from .sql? Commented Apr 18, 2013 at 18:02
  • 2
    This has a lot great information here: Improve INSERT-per-second performance of SQLite Commented Oct 8, 2021 at 21:14

2 Answers 2

1

You should use statements

database.beginTransaction();
SQLiteStatement stmt = database.compileStatement(sql);

for (int i = 0; i < NUMBER_OF_ROWS; i++) {
    //generate some values

    stmt.bindString(1, randomName);
    stmt.bindString(2, randomDescription);
    stmt.bindDouble(3, randomPrice);
    stmt.bindLong(4, randomNumber);

    long entryID = stmt.executeInsert();
    stmt.clearBindings();
}

database.setTransactionSuccessful();
database.endTransaction();

Reference

Here is one more link where a comparison between these two insertion methods is given. (for 100 records, normal insertion took 1657ms, while statements with endTransaction() took 92ms)

Sign up to request clarification or add additional context in comments.

Comments

1

I have ran into the exact same situation. First I was using DatabaseUtil.insertHelper, and it was taking me +30 seconds for 600 entries.

I did a bit more digging and found out that every row i'm inserting is being committed, which is extremely expensive.

To solve this, I switched to ORMLite, with the insertAll method:

/**
* Inserts multiple objects into the database quickly by committing them all at once
*
* @param objects The objects to insert
*/
public void insertAll(final T[] objects) {
    try {
        dao.callBatchTasks(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            for (T object : objects) {
                dao.create(object);
            }
            return null;    
        }
        });
    } catch (Exception e) {
    AdbLogger.error("Error inserting new " + className + "s in the database");
}
}

The idea here is that they do the insert of the array of object, then at the end only do one big commit. After this switch the insert became almost instantanious.

1 Comment

@wangyyif2 : Thanks a lot, i'll try this. hope it'll work :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.