Nat*_*ael 6 performance android bulkinsert ormlite
我正在尝试使用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());
}
Run Code Online (Sandbox Code Playgroud)
companies.sqlite是一个包含数千行插入的文件,如下所示:
INSERT INTO Companies (id, name) VALUES (1, 'Duff Beer');
Run Code Online (Sandbox Code Playgroud)
我正在使用DatabaseConfigUtil类来避免使用注释.
Pau*_*rke 11
使用数据库事务.请参见此处和TransactionManager.就像是:
TransactionManager.callInTransaction(connectionSource,
new Callable<Void>() {
public Void call() throws Exception {
// Your code from above
}
});
Run Code Online (Sandbox Code Playgroud)