从资产复制数据库的数据库文件夹 [英] copy database from assets to databases folder

查看:149
本文介绍了从资产复制数据库的数据库文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在主要活动我有这样的方法,从资产复制文件数据库文件夹:

 尝试{
    //检查存在与否
    SQLiteDatabase DBE = SQLiteDatabase.openDatabase(/数据/数据​​/ com.henanet.dalel /数据库/ mydb.sqlite,NULL,0);
    dbe.close();
    //副本,如果NOT EXISTS
    AssetManager AM = getApplicationContext()getAssets()。
    的OutputStream OS =新的FileOutputStream(/数据/数据​​/ com.henanet.dalel /数据库/ mydb.sqlite);
    byte []的B =新的字节[100];
    INT R;
    InputStream的是= am.open(mydb.sqlite);
    而((R = is.​​read(二))!=  -  1){
        os.write(B,O,R);
    }
    is.close();
    os.close();
}
赶上(例外五)
{

}
 

但是,一旦用户安装应用程序,他得到这个错误在LogCat中:

  09-14 22:57:25.694:I /数据库(19903):源码返回:错误code = 14,味精=无法打开文件的源代码行25467
09-14 22:57:25.694:E /数据库(19903):sqlite3_open_v2(/数据/数据​​/ com.henanet.dalel /数据库/ mydb.sqlite,和放大器;手柄,2,NULL)失败
 

解决方案

我的方法

获取使用数据库路径下

  ContextWrapper CW =新ContextWrapper(getApplicationContext());
DB_PATH = cw.getFilesDir()getAbsolutePath()+/数据库/。 //编辑,数据库
 

然后你就可以走这条路

 私人无效copyDataBase()
    {
        Log.i(数据库,
                新的数据库被复制到设备!);
        byte []的缓冲区=新的字节[1024];
        OutputStream的myOutput = NULL;
        INT长;
        //打开本地数据库作为输入流
        InputStream的myInput = NULL;
        尝试
        {
            myInput = myContext.getAssets()开(DB_NAME)。
            //传输的字节从inputfile中的
            // 输出文件
            myOutput =新的FileOutputStream(DB_PATH + DB_NAME);
            而((长度= myInput.read(缓冲液))大于0)
            {
                myOutput.write(缓冲液,0,长度);
            }
            myOutput.close();
            myOutput.flush();
            myInput.close();
            Log.i(数据库,
                    新的数据库已经被复制到设备!);


        }
        赶上(IOException异常E)
        {
            e.printStackTrace();
        }
    }
 

In main activity I have this method which copies files from assets to the databases folder:

try{
    // CHECK IS EXISTS OR NOT
    SQLiteDatabase dbe = SQLiteDatabase.openDatabase("/data/data/com.henanet.dalel/databases/mydb.sqlite",null, 0);
    dbe.close();
    // COPY IF NOT EXISTS
    AssetManager am = getApplicationContext().getAssets();
    OutputStream os = new FileOutputStream("/data/data/com.henanet.dalel/databases/mydb.sqlite");
    byte[] b = new byte[100];
    int r;
    InputStream is = am.open("mydb.sqlite");
    while ((r = is.read(b)) != -1) {
        os.write(b, 0, r);
    }
    is.close();
    os.close();
}
catch(Exception e)
{

}

But once the user installs the app, he gets this error in LogCat:

09-14 22:57:25.694: I/Database(19903): sqlite returned: error code = 14, msg = cannot               open file at source line 25467
09-14 22:57:25.694: E/Database(19903): sqlite3_open_v2("/data/data/com.henanet.dalel/databases/mydb.sqlite", &handle, 2, NULL) failed

解决方案

My method

Get Your Database path using the following

ContextWrapper cw =new ContextWrapper(getApplicationContext());
DB_PATH =cw.getFilesDir().getAbsolutePath()+ "/databases/"; //edited to databases

Then you can go this way

private void copyDataBase()
    {
        Log.i("Database",
                "New database is being copied to device!");
        byte[] buffer = new byte[1024];
        OutputStream myOutput = null;
        int length;
        // Open your local db as the input stream
        InputStream myInput = null;
        try
        {
            myInput =myContext.getAssets().open(DB_NAME);
            // transfer bytes from the inputfile to the
            // outputfile
            myOutput =new FileOutputStream(DB_PATH+ DB_NAME);
            while((length = myInput.read(buffer)) > 0)
            {
                myOutput.write(buffer, 0, length);
            }
            myOutput.close();
            myOutput.flush();
            myInput.close();
            Log.i("Database",
                    "New database has been copied to device!");


        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }

这篇关于从资产复制数据库的数据库文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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