gpt4 book ai didi

java - 安卓 : Loading data from XML to SQLite take very long time

转载 作者:行者123 更新时间:2023-12-01 12:46:20 28 4
gpt4 key购买 nike

我有一个非常大的表,有 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();
}

}

最佳答案

试试这个代码:

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

关于java - 安卓 : Loading data from XML to SQLite take very long time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24670277/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com