Thousands of ORMLite raw inserts taking several minutes on Android

让人想犯罪 __ 提交于 2019-12-01 02:33:07

问题


I'm trying to pre-populate an Android SQLite database using ORMLite. The problem is that this operation is too slow. It takes several minutes. The code below show how it 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 companies.sqlite is a file with tousands of lines of inserts like this:

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

I'm using a DatabaseConfigUtil class to avoid the use of annotations.


回答1:


Use a database transaction. See here and TransactionManager. Something like:

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


来源:https://stackoverflow.com/questions/18884587/thousands-of-ormlite-raw-inserts-taking-several-minutes-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!