Thousands of ORMLite source inserts taking a few minutes on Android

I am trying to pre-populate an Android SQLite database using ORMLite. The problem is that this operation is too slow. It takes a few minutes. The code below shows how this happens.

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

Where company.sqlite is a file with tousands of insertion lines, for example:

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

I use the DatabaseConfigUtil class to avoid using annotations.

+6
source share
1 answer

Use database transaction. See here and TransactionManager . Sort of:

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

Source: https://habr.com/ru/post/954118/


All Articles