首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Android中使用预加载的Sqlite数据库

在Android中使用预加载的Sqlite数据库
EN

Stack Overflow用户
提问于 2014-08-27 04:23:26
回答 2查看 1.3K关注 0票数 2

我想使用预装的sqlite数据库,它和一样大,250兆字节。我已经将数据库复制到资产文件夹中。由于不能直接访问资产数据库,所以我要将数据库从资产复制到"context.getFilesDir()“文件夹中。

使用这种策略,我的应用程序可以很好地工作在小数据库上,高达5-6兆字节,但是如果我这样做的话,应用程序就会崩溃,这就是我想要的。

我想做的事情有什么更好的方法吗?我已经尝试过可能的副本Android:访问带有.sqlite扩展名的资产文件夹sqlite数据库文件 从包含多个具有多列的表的.sqlite中获取列 如何在Android中使用资产预装SQLite数据库的解决方案。

我的SqliteHelper代码如下所示,以供参考。

代码语言:javascript
运行
复制
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());

        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2014-08-27 04:33:45

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

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

2-从外部文件夹将DB复制到您的internalStorage /data/data/yourApp

3-启动时检查数据库是否存在,如果没有复制

谷歌博客的限制。您可能需要在DB中使用Expansion File

票数 0
EN

Stack Overflow用户

发布于 2014-08-27 05:08:14

这段代码是通过org.apache.commons.io.IOUtils编写的。

代码语言:javascript
运行
复制
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) {}
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25518895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档