将资产数据库中的数据库复制到应用程序中

时间:2021-07-24 20:42:45

I have followed the many tutorials online on how to achieve copying a DB file from the "assets" folder in the apk to the "/data/data//databases" folder of the application. With my code it fails on the first time of opening it and copying the database over.

我已经在线阅读了很多关于如何将数据库文件从apk中的“assets”文件夹复制到应用程序的“/ data / data // databases”文件夹的教程。使用我的代码,它在第一次打开它并复制数据库时失败了。

Like the tutorials say it creates an empty database in which my database is copied into, but it doesn't seem to work for me. The code is exactly the same as everywhere else. But here it is anyway with the appropriate Logs.

就像教程说它创建一个空数据库,我的数据库被复制到其中,但它似乎不适合我。代码与其他地方完全相同。但无论如何它都适用于适当的日志。

public void createDataBase() throws IOException{

        boolean dbExist = checkDataBase();
        SQLiteDatabase db_Read = null;

        if(dbExist){
            //do nothing - database already exist
            Log.v("DBHandler", "Database does exist");
        }else{
            //By calling this method and empty database will be created into the default system path
            //of your application so we are gonna be able to overwrite that database with our database.
            Log.v("DBHandler", "Database does not exist");

            db_Read = this.getReadableDatabase(); 
            db_Read.close();

        try {
                copyDataBase();
            } catch (IOException e) {
                throw new Error("error copying database"); //Error gets thrown here
            }
        }

    }


private void copyDataBase() throws IOException{

    //Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;

    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){  
          myOutput.write(buffer, 0, length);
    }

    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

}

I get the following error in the Log:

我在日志中收到以下错误:

08-01 14:34:25.046: ERROR/Database(27530): sqlite3_open_v2("/data/data/com.package/databases/DB.sqlite", &handle, 1, NULL) failed

I am pretty certain the error is in the copying of the database over. if any more information is needed i'll gladly provide it.

我很确定错误在于复制数据库。如果需要更多信息,我很乐意提供。

EDIT: I am sure the error is in the copying of the database, all the path directories are correct. I think it has something to do with the line: InputStream myInput = myContext.getAssets().open(DB_NAME); with maybe the context passed through being wrong, but I can't see how.

编辑:我确定错误是在复制数据库,所有路径目录都是正确的。我认为它与该行有关:InputStream myInput = myContext.getAssets()。open(DB_NAME);也许上下文通过了错误,但我看不出如何。

1 个解决方案

#1


0  

You copy database using regular file operations while error message clearly refers to sqlite. So the error most likely occurs when you try to open non existing database. If there is a stack trace it will show exactly where it happens.

您使用常规文件操作复制数据库,而错误消息明确指向sqlite。因此,当您尝试打开非现有数据库时,最有可能发生错误。如果有堆栈跟踪,它将准确显示它发生的位置。

Finally, I recommend that you don't use class Error as it's reserved for VM errors. Use RuntimeException and always include root cause in it.

最后,我建议您不要使用类Error,因为它是为VM错误保留的。使用RuntimeException并始终包含根本原因。

#1


0  

You copy database using regular file operations while error message clearly refers to sqlite. So the error most likely occurs when you try to open non existing database. If there is a stack trace it will show exactly where it happens.

您使用常规文件操作复制数据库,而错误消息明确指向sqlite。因此,当您尝试打开非现有数据库时,最有可能发生错误。如果有堆栈跟踪,它将准确显示它发生的位置。

Finally, I recommend that you don't use class Error as it's reserved for VM errors. Use RuntimeException and always include root cause in it.

最后,我建议您不要使用类Error,因为它是为VM错误保留的。使用RuntimeException并始终包含根本原因。