sql崩溃

velaa5lx  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(508)

我正在尝试将应用程序中生成的数据库文件备份到 .zip 文件,然后将其解压缩以还原到 /data/data/<packagename>/databases/ 文件夹。我只压缩数据库文件,而不是 -journal files . 当我单击恢复选项时 .db 文件被解压并移动到 /databases/ 文件夹。问题是我在移动文件时崩溃了。下面是错误日志:

E/UncaughtException: android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database (code 1032)
                                                                          #################################################################
                                                                          Error Code : 1032 (SQLITE_READONLY_DBMOVED)
                                                                          Caused By : Database or Journal file have been removed.
                                                                            (attempt to write a readonly database (code 1032))
                                                                          #################################################################
                                                                              at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)

解压后移动文件的函数:

public void moveContents(){
        File destinationFolder = new File(DATABASE_FOLDER_PATH);
        File sourceFolder = new File(Environment.getExternalStorageDirectory() + "/AppName/Backups/_extracts");
        if (!destinationFolder.exists())
        {
            destinationFolder.mkdirs();
        }
        // Check weather source exists and it is folder.
        if (sourceFolder.exists() && sourceFolder.isDirectory())
        {
            // Get list of the files and iterate over them
            File[] listOfFiles = sourceFolder.listFiles();
            if (listOfFiles != null)
            {
                for (File child : listOfFiles )
                {
                    try {
                        File checkFile=new File(destinationFolder+"/"+child.getName());
                        if(checkFile.exists()){
                            checkFile.delete();
                            FileUtils.moveFileToDirectory(child,destinationFolder,false);
                        }else{
                            FileUtils.moveFileToDirectory(child,destinationFolder,false);
                        }

                    } catch (IOException e) {
                        Log.e("FLERROR","ERROR",e);
                    }
                    // Move files to destination folder
                }

                // Add if you want to delete the source folder
                sourceFolder.delete();
            }
        }

        else
        {
            System.out.println(sourceFolder + "  Folder does not exists");
        }
    }

应用程序正在崩溃并已关闭。
请帮忙。提前谢谢。

5gfr0r5j

5gfr0r5j1#

根据您的错误日志,似乎您正在尝试移动只读的数据库。你必须获得数据库的可写权限。
所以在移动数据库之前。使用此命令使其可写。

String yourpath = DB_PATH + DATABASE_NAME;
SQLiteDatabase yourdb = SQLiteDatabase.openDatabase(yourpath, null, SQLiteDatabase.OPEN_READWRITE);

相关问题