2013-01-09 83 views
2

在我的應用程序數據庫,我通過複製了已儲存在我的資產文件夾中現有的SQLite數據庫的數據庫。安卓果凍豆:損壞的升級

在代碼我還檢查數據庫的版本,因此,如果應用程序的較新版本安裝有更高數據庫版本,則數據庫重新創建(在onUpgrade方法)。

我遇到的問題是,升級過程完全運作以往的Android版本,但由於安裝果凍豆,我已經越來越數據庫已損壞的錯誤。如果我清理安裝應用程序(因此不需要升級數據庫,只需創建),那麼應用程序也可以正常運行。

我測試了這兩個設備是用4.1.1 S3,和一個的Nexus 4與4.2.1。

任何想法,爲什麼它會啓動失敗上最新的Android版本?謝謝!

的錯誤如下:

01-09 09:59:50.941: E/SQLiteLog(15915): (11) database corruption at line 51776 of [00bb9c9ce4] 
01-09 09:59:50.941: E/SQLiteLog(15915): (11) database disk image is malformed 
01-09 09:59:50.941: E/DefaultDatabaseErrorHandler(15915): Corruption reported by sqlite on database: /data/data/myapp/databases/services.sqlite 
01-09 09:59:50.941: E/DefaultDatabaseErrorHandler(15915): deleting the database file: /data/data/myapp/databases/services.sqlite 
01-09 09:59:50.991: E/SQLiteLog(15915): (1) no such table: services 
01-09 09:59:50.991: D/AndroidRuntime(15915): Shutting down VM 
01-09 09:59:50.991: W/dalvikvm(15915): threadid=1: thread exiting with uncaught exception (group=0x40c3c930) 
01-09 09:59:50.991: E/AndroidRuntime(15915): FATAL EXCEPTION: main 
01-09 09:59:50.991: E/AndroidRuntime(15915): java.lang.RuntimeException: Unable to start activity ComponentInfo{myapp/myapp.MainActivity}: android.database.sqlite.SQLiteException: no such table: services (code 1): , while compiling: (my query) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at android.os.Handler.dispatchMessage(Handler.java:99) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at android.os.Looper.loop(Looper.java:137) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at android.app.ActivityThread.main(ActivityThread.java:5039) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at java.lang.reflect.Method.invokeNative(Native Method) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at java.lang.reflect.Method.invoke(Method.java:511) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at dalvik.system.NativeStart.main(Native Method) 
01-09 09:59:50.991: E/AndroidRuntime(15915): Caused by: android.database.sqlite.SQLiteException: no such table: services (code 1): , while compiling: (my query) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at myapp.c.f(Unknown Source) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at myapp.bk.a(Unknown Source) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at myapp.MainActivity.onCreate(Unknown Source) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at android.app.Activity.performCreate(Activity.java:5104) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
01-09 09:59:50.991: E/AndroidRuntime(15915): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
01-09 09:59:50.991: E/AndroidRuntime(15915): ... 11 more 

代碼來創建和升級數據庫如下:

public void createDatabase() throws IOException { 
    boolean dbExist = checkDatabase(); 

    if(dbExist) { 
     this.getWritableDatabase(); 
    } 

    dbExist = checkDatabase(); 
    SQLiteDatabase db_Read = null; 

    if(!dbExist) { 
     db_Read = this.getReadableDatabase(); 
     try { 
      copyDatabase(); 
     } catch(IOException e) { 
      throw new Error ("Error copying database"); 
     } 
    } 
} 

private boolean checkDatabase(){ 

    SQLiteDatabase checkDB = null; 

    try{ 
     String myPath = DB_PATH + DB_NAME; 
     checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 
    }catch(SQLiteException e){ 
     //database does't exist yet. 
    } 

    if(checkDB != null){ 
     checkDB.close(); 
    } 

    return checkDB != null ? true : false; 
} 

private void copyDatabase() throws IOException { 

    InputStream myInput = myContext.getAssets().open(DB_NAME); 

    String outFileName = DB_PATH + DB_NAME; 
    OutputStream myOutput = new FileOutputStream(outFileName); 

    byte[] buffer = new byte[1024]; 
    int length; 
    while((length = myInput.read(buffer))>0) { 
     myOutput.write(buffer, 0, length); 
    } 
    myOutput.flush(); 
    myOutput.close(); 
    myInput.close(); 
} 


public SQLiteDatabase openDatabase() throws SQLException { 
    String myPath = DB_PATH + DB_NAME; 
    myDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 
    return myDatabase; 
} 


@Override 
public synchronized void close() { 
    if(myDatabase != null) { 
     myDatabase.close(); 
    } 
    super.close(); 
} 

@Override 
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) { 
    sc.updatedDB = true; 
    if (newVersion > oldVersion) { 
     try { 
      copyDatabase(); 
     } catch (IOException e) { 
      throw new Error("Error upgrading database"); 
     } 
    } 
} 
+0

你onUpgrade方法很奇怪。以及您複製數據庫文件而不是添加新表和選擇/插入數據的事實。 – vorrtex

+0

我不介意在升級時將數據保存在數據庫中,我只需要更新結構(表,列),因此我想從資產文件夾中的sqlite數據庫複製新結構。 – jay

回答

0

錯誤日誌說,你應該有一個表名「服務「它沒有找到它,檢查你的數據庫創建代碼,也許你在創建查詢時犯了一個錯誤。

+0

感謝您的迴應!最初創建數據庫的相同代碼在升級後用於重新創建它。但是,由於它在清潔數據庫創建時正確創建了「服務」表,因此我認爲問題不在於此。 – jay

+0

看看這個答案,也許它會幫助你解決不存在的表的問題:[link](http://stackoverflow.com/a/4991669/1348478) –

0

問題是在你的onUpgrade方法。它不工作。你不能用onUpdate方法覆蓋新文件。第一個參數是一個數據庫(SQLiteDatabase數據庫),它已經打開。所以你不能用你的新文件覆蓋打開的數據庫文件。在debbuger中檢查這個。

也許你有其他錯誤,在你的代碼...

1

您可以通過將一個接近()語句在COPYDB方法創建輸出流之前避免這個問題:

InputStream myInput = myContext.getAssets().open(DB_NAME); 

String outFileName = DB_PATH + DB_NAME; 

    if(myDataBase != null) 
     myDataBase.close(); 

OutputStream myOutput = new FileOutputStream(outFileName);