将数据库从资产复制到数据库文件夹[副本]

时间:2021-12-18 00:27:07

This question already has an answer here:

这个问题已经有了答案:

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:

但是一旦用户安装了这个应用,他会在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

1 个解决方案

#1


16  

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();
        }
    }

#1


16  

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();
        }
    }