性能SQLite的问题 - 一个codechange能加快东西呢? [英] Performance SQLite Issue - Can a codechange speed up things?

查看:105
本文介绍了性能SQLite的问题 - 一个codechange能加快东西呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code将行添加到我的数据库:

I use the following code to add rows to my database :

    public void insert(String kern, String woord) {
      SQLiteDatabase db = getWritableDatabase();

      ContentValues values = new ContentValues();
      values.put(KERN, kern);
      values.put(WOORD, woord);

      db.insertOrThrow(TABLE_NAME, null, values);
      return;

目前,我正在调用该插件()3.455倍,所有单词添加到数据库,使用:插入(水果,香蕉);它永远。

Currently, I'm invoking this insert() 3.455 times, to add all words to the database, using : insert("Fruits", "Banana"); It takes forever.

我怎样才能改变这种code的工作速度更快?我想在的foreach行,但不知道如何实现..谢谢!

How can I change this code to work faster? I'm thinking in the line of foreach, but don't know how to implement.. Thanks!

/编辑;该解决方案由@hovanessyan作品提供,将做的工作。和..请注意,如果你有很多都有被放在行,你可能会使用该方法超过最大字节限制误差面对。在这种情况下,查看其他的解决方案,这意味着在实际.apk文件打包到数据库中。

/Edit; The solution provided by @hovanessyan works and will do the job. AND.. note that if you have a lot of lines that have to be put in, you might be confronted with the method exceeds max byte limit error. In that case, review the other solution, that suggests packing the database in the actual .APK file.

推荐答案

同时使用hovanessyan和达米安的提示(只要我达到15提醒我要代表+ 1你),我想出了以下解决方案:

Using the tips of both hovanessyan and Damian (remind me to rep+1 you as soon as I reach 15 ;), I came up with the following solution:


  1. 对于相对小型数据库(小于1,5MB)

我创建使用SQLite数据库浏览器数据库,并把它放在我的资产文件夹中。

I created the database using SQLite Database Browser, and put it in my Assets folder.

然后,下面code拷贝数据库设备,如果它已不存在:

Then, the following code copies the database to the device, if it's not already there:

    boolean initialiseDatabase = (new File(DB_DESTINATION)).exists();

公共无效COPYDB()抛出IOException
        最后弦乐DB_DESTINATION =/data/data/happyworx.nl.Flitswoorden/databases/WoordData.db;

public void copyDB() throws IOException{ final String DB_DESTINATION = "/data/data/happyworx.nl.Flitswoorden/databases/WoordData.db";

    // Check if the database exists before copying


    Log.d("Database exist", "" + initialiseDatabase);
    Log.d("Base Context", "" + getBaseContext());

    if (initialiseDatabase == false) {

        // Open the .db file in your assets directory
        InputStream is = getBaseContext().getAssets().open("WoordData.db");


        // Copy the database into the destination
        OutputStream os = new FileOutputStream(DB_DESTINATION);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = is.read(buffer)) > 0){
            os.write(buffer, 0, length);
        }
        os.flush();

        os.close();
        is.close();
    }}

在我的应用程序,数据库的一部分是用户自定义的。

In my app, a portion of the database is User-customizable.

我称之为code以上在onStart()有:

I call the code above in onStart() with :

        try {
        copyDB();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

所以,当用户presses复位数据库标准(以preferences屏),我只是设置布尔 initialiseDatabase 假并等待用户返回到主活性。 (因此调用的OnStart和复制原始数据库)。

So, when the user presses "reset database to standard" (in preferences screen), I just set the Boolean initialiseDatabase to "false" and wait for the user to go back to the main activity. (thus calling onstart and copying the original database).

我试图从preferences.java调用Activity.copyDB()。它是整洁的,因为它不要求用户返回到主活动来重建数据库。不过,我得到不能够静态引用调用非静态方法错误。我不明白这一点,但会考虑它。

I tried to call the Activity.copyDB() from the preferences.java. It's neater, because it doesn't require the user to go back to the main activity to rebuild the database. However, I get an error about not being able to call static references to non-static methods. I don't understand that, but will look into it.

这篇关于性能SQLite的问题 - 一个codechange能加快东西呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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