Android faster database insert

匿名 (未验证) 提交于 2019-12-03 08:54:24

问题:

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:

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(); } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!