Jin
Jin

Reputation: 6145

(Android) Inserting thousands of rows to sqlite

I am having trouble inserting huge amount of data into the Android sqlite database (~7000 rows of data). I am using InsertHelper, but it doesn't seem to speed it up that much. Is there any other way to efficiently insert that much data into the db?

Thanks.

Edit:

I found a fix for it. My insert code follows (details omitted for conciseness):

public void addEntries(ArrayList<KeywordMap> keywords) {
        if (keywords == null || keywords.size() == 0)
            return;

        mDb.beginTransaction();

        String sql = "Insert into " + AUTOCOMPLETE_TABLE + " (path, name, course_id, " +
                "course_id_norm, type, year) values(?,?,?,?,?,?)";
        SQLiteStatement insert = mDb.compileStatement(sql);

        for (KeywordMap keyword : keywords) {
            insert.bindString(1, keyword.path);
            insert.bindString(2, keyword.name);
            insert.bindString(3, keyword.cid);
            insert.bindString(4, keyword.cid_norm);
            insert.bindDouble(5, keyword.type);
            insert.bindDouble(6, keyword.year);

            insert.execute();
        }

        mDb.setTransactionSuccessful();
        mDb.endTransaction();
    }

Upvotes: 1

Views: 1063

Answers (1)

Report Error
Report Error

Reputation: 414

db.beginTransaction()

//insert, insert, insert … n-insert

db.setTransactionSuccessful();
db.endTransaction();

Upvotes: 9

Related Questions