在Android中使用预装的Sqlite数据库

时间:2014-08-27 04:23:27

标签: android database sqlite android-assets

我想使用预加载的sqlite数据库,该数据库大到 250兆字节。我已将数据库复制到 assets 文件夹中。由于我们无法直接访问资产数据库,因此我将数据库从资产复制到 “ context.getFilesDir()”文件夹。
使用这个策略我的应用程序适用于小型数据库高达5-6兆字节,但如果我这样做的大型数据库应用程序崩溃,这是我想要的。

我正在尝试做什么有更好的方法或错误吗? 我已经尝试过可能副本的解决方案 Android: Accessing assets folder sqlite database file with .sqlite extension Getting a column from .sqlite containing multiple tables with multiple columns How to use preloaded SQLite database in Android from Assets

我在下面给出了SqliteHelper的代码以供参考。

package com.example.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;

/**
 * Created by MOHIT on 06-08-2014.
 */
public class SQLiteHelper  {
    private static final String DB_NAME = "alldb.sqlite";
    //private String DB_NAME = "alldb.sqlite";

    private Context context;
    private SQLiteDatabase database;
    public SQLiteHelper(Context context) {
        this.context = context;
    }

    public SQLiteDatabase openDatabase() {
        File dbFile = new File(context.getFilesDir(), DB_NAME);

        if (!dbFile.exists()) {
            try {
                copyDatabase(dbFile);
            } catch (IOException e) {
           //     Utilities.makeToastLong(context, "Error here");
                throw new RuntimeException("Error creating source database", e);

            }
        }

        database= SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.OPEN_READONLY);

        return database;

    }
    public void closeDatabase()
    {
        if(database!=null&& database.isOpen())
          database.close();
    }

    private void copyDatabase(File dbFile) throws IOException {

        try {

            InputStream is = context.getAssets().open(DB_NAME);
            dbFile.createNewFile();
            OutputStream os = new FileOutputStream(dbFile,false);

            byte[] buffer = new byte[1024];
            while (is.read(buffer) > 0) {
                os.write(buffer);
            }

            os.flush();
            os.close();
            is.close();
        }catch (Exception e)
        {
            Log.e(SQLiteHelper.class.getSimpleName()," Error in Database copy "+e.getLocalizedMessage());

        }
    }
}

2 个答案:

答案 0 :(得分:0)

我在很多其他主题中读到预加载的SQLite DB的大小限制是50兆字节。要绕过此限制,您可以:

1 - 将数据库复制到外部文件夹

2 - 从外部文件夹中,将数据库复制到internalStorage / data / data / yourApp

如果数据库存在,则在启动时进行3次检查,如果不存在则

对Google Blog的限制。您可能需要使用Expansion File作为数据库

答案 1 :(得分:0)

此代码,通过org.apache.commons.io.IOUtils。

public static void initialize(Context context) {
    File dbFile = getDatabaseAbsolutePath(context);
    if (!dbFile.exists()) {
        if (Ln.isDebugEnabled()) Ln.i("###### Copying preloaded DATABASE to: "+dbFile.getAbsolutePath());
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            is = context.getAssets().open("init.db");
            fos = new FileOutputStream(dbFile);
            IOUtils.copy(is, fos);
        } catch (Exception e) {
            if ( Ln.isDebugEnabled()) e.printStackTrace();
        } finally {
            try {
                if (is != null) is.close();
                if (fos != null) fos.close();
            } catch(Exception e) {}
        }
    }
}