簡體   English   中英

在某些設備上復制后,Sqlite數據庫已損壞

[英]Sqlite database is corrupted after copy on some devices android

我是android開發的新手,這是我的第一個帶有數據庫的應用程序。 我在Ubuntu下使用DB Browser for SQlite創建了一個數據庫。

似乎在棉花糖以下的Android設備上存在問題,但我不確定。

這是我復制數據庫的代碼:

//version number.
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "db.sqlite";
private static String DATABASE_PATH = "/data/data/com.andrea.risuscito_passaggi/databases/";

private SQLiteDatabase myDataBase;
private final Context myContext;

public DBHelper(Context context ) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.myContext = context;
}


/**
 * Copies your database from your local assets-folder to the just created empty database in the
 * system folder, from where it can be accessed and handled.
 * This is done by transfering bytestream.
 * */
public void copyDataBase() throws IOException{

    boolean bCopyOk = false;
    try{
        //Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
        // Path to the just created empty db
        String outFileName = DATABASE_PATH + DATABASE_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))>-1) myOutput.write(buffer, 0, length);
        //Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();
        bCopyOk = true;
    }catch(Exception ex){
        ex.printStackTrace();
    }finally{
        if (bCopyOk) Log.v(TAG, "[copyDataBase] - Database copied OK!");
    }
}

在所有裝有Android Nougat(7.0.x)的設備上,一切正常,但是,如果我在另一個版本的android上測試了此應用程序,則會發現此錯誤日志:

03-22 20:08:21.660 9010-9010/com.andrea.risuscito_passaggi E/SQLiteLog: (14) cannot open file at line 31278 of [2ef4f3a5b1]
03-22 20:08:21.661 9010-9010/com.andrea.risuscito_passaggi E/SQLiteLog: (14) os_unix.c:31278: (2) open(/data/data/com.andrea.risuscito_passaggi/databases/db.sqlite) - 
03-22 20:08:21.661 9010-9010/com.andrea.risuscito_passaggi E/SQLiteDatabase: Failed to open database '/data/data/com.andrea.risuscito_passaggi/databases/db.sqlite'.
                                                                             android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
                                                                                 at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
                                                                                 at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:207)
                                                                                 at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191)
                                                                                 at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
                                                                                 at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
                                                                                 at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
                                                                                 at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:845)
                                                                                 at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:830)
                                                                                 at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:723)
                                                                                 at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:690)
                                                                                 at com.andrea.risuscito_passaggi.DBHelper.openDataBase(DBHelper.java:154)
                                                                                 at com.andrea.risuscito_passaggi.MainActivity.onCreate(MainActivity.java:94)
                                                                                 at android.app.Activity.performCreate(Activity.java:6304)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2539)
                                                                                 at android.app.ActivityThread.access$900(ActivityThread.java:159)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:152)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5507)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-22 20:08:21.662 9010-9010/com.andrea.risuscito_passaggi W/SQLiteDatabase: Retry to open database[/data/data/com.andrea.risuscito_passaggi/databases/db.sqlite] due to error: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
03-22 20:08:21.662 9010-9010/com.andrea.risuscito_passaggi E/SQLiteLog: (14) cannot open file at line 31278 of [2ef4f3a5b1]
03-22 20:08:21.662 9010-9010/com.andrea.risuscito_passaggi E/SQLiteLog: (14) os_unix.c:31278: (2) open(/data/data/com.andrea.risuscito_passaggi/databases/db.sqlite) - 
03-22 20:08:21.663 9010-9010/com.andrea.risuscito_passaggi E/SQLiteDatabase: Failed to open database '/data/data/com.andrea.risuscito_passaggi/databases/db.sqlite'.
                                                                             android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
                                                                                 at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
                                                                                 at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:207)
                                                                                 at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191)
                                                                                 at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
                                                                                 at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
                                                                                 at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
                                                                                 at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:845)
                                                                                 at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:830)
                                                                                 at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:732)
                                                                                 at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:690)
                                                                                 at com.andrea.risuscito_passaggi.DBHelper.openDataBase(DBHelper.java:154)
                                                                                 at com.andrea.risuscito_passaggi.MainActivity.onCreate(MainActivity.java:94)
                                                                                 at android.app.Activity.performCreate(Activity.java:6304)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2539)
                                                                                 at android.app.ActivityThread.access$900(ActivityThread.java:159)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:152)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5507)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我已經搜索了很多,但沒有找到任何解決方案。 謝謝

編輯:我停止尋找僅在Java代碼中的錯誤,我開始調查數據庫代碼。 我發現了兩個錯誤:

  1. 沒有創建到空數據庫的文件夾,因此我在復制數據庫之前添加了此代碼

    // check if databases folder exists, if not create one and its subfolders File databaseFile = new File( DATABASE_PATH); if (!databaseFile.exists()){ databaseFile.mkdir(); }

  2. 數據庫中有一個被破壞的視圖,我無法修復它,但是我通過從Java編寫查詢來解決

我停止只在Java代碼中查找錯誤,然后開始研究數據庫代碼。 我發現了兩個錯誤:

1.沒有創建到空數據庫的文件夾,所以我在復制數據庫之前添加了此代碼

// check if databases folder exists, if not create one and its subfolders
            File databaseFile = new File( DATABASE_PATH);
            if (!databaseFile.exists()){
                databaseFile.mkdir();
            }
  1. 數據庫中有一個被破壞的視圖,我無法修復它,但是我通過從Java編寫查詢來解決

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM