简体   繁体   English

将数据库从资产文件夹复制到应用程序

[英]Copying Database from Assets Folder into app

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. 我已在线上关注了许多教程,这些教程如何实现将apk中的“ assets”文件夹中的数据库文件复制到应用程序的“ / data / data // databases”文件夹中。 With my code it fails on the first time of opening it and copying the database over. 使用我的代码,它在第一次打开它并复制数据库时会失败。

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); 我认为这与以下行有关: InputStream myInput = myContext.getAssets().open(DB_NAME); with maybe the context passed through being wrong, but I can't see how. 也许上下文是通过错误传递的,但我看不出是怎么回事。

You copy database using regular file operations while error message clearly refers to sqlite . 您使用常规文件操作复制数据库,而错误消息显然指向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. 如果有堆栈跟踪,它将确切显示它发生的位置。

Finally, I recommend that you don't use class Error as it's reserved for VM errors. 最后,我建议您不要使用Error类,因为它是为VM错误保留的。 Use RuntimeException and always include root cause in it. 使用RuntimeException并始终在其中包含根本原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM