Android faster database insert

2019-05-08 19:22发布

I am parsing a HUGE xml which contains nearly 20000 tags, I am saving all the entries from the xml into my sqlite database as soon as i am parsing them.

But apparently xml is getting parsed pretty quickly without insert operation, but when i try to insert each values it takes very long time.( Around 10 mins).

Database insert code :

for (int i = 0; i < tracksList.size(); i++) {
    dataSource.addTracks(tracksList.get(i));                        
}



public long addTracks(Tracks tracks) {

    long insertId = 0;

    ContentValues values = new ContentValues();
    values.put(TRACKS_ID, tracks.getStrId());
    values.put(TRACKS_ARTISTID, tracks.getStrArtistId());
    values.put(TRACKS_ARTISTNAME, tracks.getStrArtistName());
    values.put(TRACKS_ALBUMNAME, tracks.getStrAlbumName());
    values.put(TRACKS_FILENAME, tracks.getStrFileName());
    values.put(TRACKS_TRACKNAME, tracks.getStrTrackName());

    insertId = database.insert(TRACKS_TABLE, null, values);

    return insertId;
}

Is there any alternative to get each element from my arraylist and save them with much better speed.

EDIT : Got it working, thnx all..

    database.beginTransaction();
    try {
        //standard SQL insert statement, that can be reused
        SQLiteStatement insert = 
                database.compileStatement("insert into " + TRACKS_TABLE
                    + "(" + TRACKS_ID + "," + TRACKS_ARTISTID
                    + "," + TRACKS_ARTISTNAME 
                    + "," + TRACKS_ALBUMNAME 
                    + "," + TRACKS_FILENAME 
                    + "," + TRACKS_TRACKNAME + ")"
                    +" values " + "(?,?,?,?,?,?)");

        for (Tracks value : tracksList){
            //bind the 1-indexed ?'s to the values specified

            System.out.println(value.getStrId());

            insert.bindLong(1, value.getStrId());
            insert.bindString(2, value.getStrArtistId());
            insert.bindString(3, value.getStrArtistName());
            insert.bindString(4, value.getStrAlbumName());
            insert.bindString(5, value.getStrFileName());
            insert.bindString(6, value.getStrTrackName());
            insert.execute();
        }
        database.setTransactionSuccessful();

    } finally {
        database.endTransaction();
    }

1条回答
在下西门庆
2楼-- · 2019-05-08 19:49

Use one transaction for all inserts, otherwise you get the storage synchronization overhead for every single insert:

database.beginTransaction();
try {
    for (int i = 0; i < tracksList.size(); i++)
        dataSource.addTracks(tracksList.get(i));                        

    database.setTransactionSuccessful();
} finally {
    database.endTransaction();
}
查看更多
登录 后发表回答