我有非常大的表300,000条记录 我正在从XML文件加载到SQLite,
不幸的是,这需要很长时间(花了15分钟然后我不得不停下来)。
这是正常的吗?
我的代码有什么问题。
如何加快速度?
感谢
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_filter);
new Thread(new Runnable() {
public void run() {
FillLocations();
}
}).start();
}
private void FillLocations()
{
OpenDB();
Resources res = getResources();
ContentValues _Values = new ContentValues();
//Open xml file
XmlResourceParser _xml = res.getXml(R.xml.locations);
try
{
//Check for end of document
int eventType = _xml.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
//Search for record tags
if ((eventType == XmlPullParser.START_TAG) &&(_xml.getName().equals("Cities"))){
//Record tag found, now get values and insert record
String City = _xml.getAttributeValue(null, "City");
String Country = _xml.getAttributeValue(null, "Country");
String Time = _xml.getAttributeValue(null, "Time");
_Values.put("City", City);
_Values.put("Country", Country );
_Values.put("Time", Time);
db.insert("Locations", null, _Values);
}
eventType = _xml.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
答案 0 :(得分:1)
试试这段代码:
在while循环 beginTransaction 之前以及成功插入所有行 endSuccessfullTransaction 之后。
public static void beginTransaction()
{
final SQLiteDatabase db = openDatabase();
if (db != null)
{
db.beginTransaction();
}
}
public static void endSuccessfullTransaction()
{
final SQLiteDatabase db = openDatabase();
db.setTransactionSuccessful();
db.endTransaction();
}