我正在尝试使用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类来避免使用注释.
解决方法
使用数据库事务.见
here和
TransactionManager.类似于:
TransactionManager.callInTransaction(connectionSource,new Callable<Void>() { public Void call() throws Exception { // Your code from above } });