问题描述
我解析,其中包含近20000标记的大型XML,我只要我解析这些数据保存在XML中的所有条目进入我的SQLite数据库。
但显然XML是越来越解析pretty没有迅速插入操作,但是当我尝试插入每个值需要花费很长的时间。(约10分钟)。
数据库插入code:
的for(int i = 0; I< tracksList.size();我++){
dataSource.addTracks(tracksList.get(I));
}众长addTracks(曲目的曲目){ 长insertId = 0; ContentValues值=新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,空,价值); 返回insertId;
}
有没有替代从我的ArrayList中获取的每个元素,并将它们保存有更好的速度。
编辑:得到它的工作,所有的日Thnx ..
database.beginTransaction();
尝试{
//标准的SQL INSERT语句,可重复使用
SQLiteStatement插入=
database.compileStatement(插入+ TRACKS_TABLE
+(+ TRACKS_ID +,+ TRACKS_ARTISTID
+,+ TRACKS_ARTISTNAME
+,+ TRACKS_ALBUMNAME
+,+ TRACKS_FILENAME
+,+ TRACKS_TRACKNAME +)
+值+(,,,,,??????)); 对于(曲目值:tracksList){
//绑定1索引?s到指定的值 的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(); } {最后
database.endTransaction();
}
使用一个交易的所有插入,否则你得到的存储同步开销为每一个INSERT语句:
database.beginTransaction();
尝试{
的for(int i = 0; I< tracksList.size();我++)
dataSource.addTracks(tracksList.get(I)); database.setTransactionSuccessful();
} {最后
database.endTransaction();
}
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();
}
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();
}
这篇关于Android的更快数据库插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!