【问题标题】:Using Preloaded Sqlite Database in Android在 Android 中使用预加载的 Sqlite 数据库
【发布时间】:2014-10-20 13:12:52
【问题描述】:

我想使用 250 兆字节 的预加载 sqlite 数据库。我已将数据库复制到 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());

        }
    }
}

【问题讨论】:

  • 应对时是否崩溃?
  • 崩溃?如何?我们能看到崩溃吗?
  • @DIVA 是的,它正在与 SQLiteDatabaseCorruptException developer.android.com/reference/android/database/sqlite/… 一起崩溃
  • 我猜复制有问题..它是否适用于小型数据库?你最近在 db 上做过任何升级吗?
  • 没有对 Db 进行升级,是的,它非常适合小型 DB。

标签: android database sqlite android-assets


【解决方案1】:

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

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

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

3-在启动时检查数据库是否存在,如果不存在则复制它

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

【讨论】:

  • 什么样的外部文件夹?我正在使用资产文件夹。
  • 您不能使用 assets 文件夹来存储您的数据库。查看我的编辑
【解决方案2】:

这个代码,通过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) {}
        }
    }
}

【讨论】:

  • 顺便说一句,Ln 是来自 roboguice 的日志库。您可以使用自己喜欢的日志记录进行更改。
  • 我在资产路径中有 init.db。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-25
  • 1970-01-01
  • 2016-04-12
  • 2020-06-12
相关资源
最近更新 更多