gpt4 book ai didi

android - 数以千计的 ORMLite 原始插入在 Android 上花费几分钟

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:37:43 24 4
gpt4 key购买 nike

我正在尝试使用 ORMLite 预填充 Android SQLite 数据库。问题是这个操作太慢了。这需要几分钟。下面的代码显示了它是如何发生的。

    RuntimeExceptionDao<Company, Integer> companyDao = ORMLiteHelper.getInstance(context).getCompanyRuntimeDao();AssetManager am = context.getAssets();

try {
InputStream instream = am.open("companies.sqlite");
if (instream != null) {
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);

String line;
try {
while ((line = buffreader.readLine()) != null) {
//Log.i("SQL", line);
if (line.startsWith("INSERT INTO Companies")) {
companyDao.executeRaw(line);
}

}
} catch (Exception e) {
Log.i("SQL", e.getMessage() + " " + e.getLocalizedMessage());
}
}
} catch (Exception e) {
Log.i("SQL", e.getMessage());
}

companies.sqlite 是一个包含大量插入行的文件,如下所示:

INSERT INTO Companies (id, name) VALUES (1, 'Duff Beer');

我正在使用 DatabaseConfigUtil 类来避免使用注释。

最佳答案

使用数据库事务。参见 hereTransactionManager .像这样的东西:

TransactionManager.callInTransaction(connectionSource,
new Callable<Void>() {
public Void call() throws Exception {
// Your code from above
}
});

关于android - 数以千计的 ORMLite 原始插入在 Android 上花费几分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18884587/

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