成千上万的ORMLite原料插入承担的Andr​​oid几分钟 [英] Thousands of ORMLite raw inserts taking several minutes on Android

查看:190
本文介绍了成千上万的ORMLite原料插入承担的Andr​​oid几分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想$ P $使用ORMLite对填充一个Android SQLite数据库。的问题是,这种操作是太慢了。这需要几分钟的时间。下面的code展示它是如何发生的。

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

在哪里companies.sqlite是用线插入像这样的tousands文件:

Where companies.sqlite is a file with tousands of lines of inserts like this:

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

我使用的是DatabaseConfigUtil类来避免使用注解。

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

推荐答案

使用数据库事务。请参见这里和<一href="http://ormlite.com/javadoc/ormlite-core/com/j256/ormlite/misc/TransactionManager.html">TransactionManager.是这样的:

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

这篇关于成千上万的ORMLite原料插入承担的Andr​​oid几分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆