升级数据库后的SQLiteDatabaseCorruptException

时间:2013-01-29 03:02:40

标签: android android-sqlite

我升级了我的数据库,它可以在几部手机上完美运行。但是,在其他情况下,我得到SQLiteDatabaseCorruptException。

  • 我已将android_metadata表包含在我的数据库中。
  • 我没有更改此更新的任何其他应用程序部分。所以,查询应该没有问题。
  • 我使用SQLite Expert创建我的数据库。

错误报告:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.quant.aptitude/com.quant.aptitude.QuestionsActivity}: android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed (code 11)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed (code 11)
at android.database.sqlite.SQLiteConnection.nativeExecuteForCursorWindow(Native Method)
at android.database.sqlite.SQLiteConnection.executeForCursorWindow(SQLiteConnection.java:838)
at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:836)
at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62)
at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:143)
at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133)
at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:196)
at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:236)
at com.quant.aptitude.l.a(Unknown Source)
at com.quant.aptitude.QuestionsActivity.a(Unknown Source)
at com.quant.aptitude.QuestionsActivity.onCreate(Unknown Source)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
... 11 more

用于创建和升级数据库的代码

public void createDataBase() throws IOException 
{ 
    boolean mDataBaseExist = checkDataBase();
    if(mDataBaseExist)
    {
        this.getWritableDatabase();
    }
    mDataBaseExist = checkDataBase();


    if(!mDataBaseExist) 
    { 
        this.getReadableDatabase(); 
        this.close(); 
        try  
        { 
            //Copy the database from assets 
            copyDataBase(); 
        }  
        catch (IOException mIOException)  
        { 
            throw new Error("ErrorCopyingDataBase"); 
        } 
    } 
} 

private boolean checkDataBase() 
{ 
    File dbFile = new File(DB_PATH + DB_NAME); 
    return dbFile.exists(); 
} 

private void copyDataBase() throws IOException 
{ 
    InputStream mInput = mContext.getAssets().open(DB_NAME); 
    String outFileName = DB_PATH + DB_NAME; 
    OutputStream mOutput = new FileOutputStream(outFileName); 
    byte[] mBuffer = new byte[1024]; 
    int mLength; 
    while ((mLength = mInput.read(mBuffer))>0) 
    { 
        mOutput.write(mBuffer, 0, mLength); 
    } 
    mOutput.flush(); 
    mOutput.close(); 
    mInput.close(); 
} 

public boolean openDataBase() throws SQLException 
{ 
    String mPath = DB_PATH + DB_NAME; 
    mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY); 
    return mDataBase != null; 
} 

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

@Override
public void onCreate(SQLiteDatabase arg0) {
    // TODO Auto-generated method stub
}

@Override  
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
    if (oldVersion < newVersion) {  
        try { 
            copyDataBase(); 
        } catch (IOException e) { 
            throw new Error("Error upgrading database"); 
        } 
    } 
}  

OnUpgrade方法是否有错误?

1 个答案:

答案 0 :(得分:0)

我相信你仍然需要添加SQL命令来在onCreate中创建空表,即使你打算覆盖它。在播种这样的数据库时,Android对于模式不匹配很挑剔。您的数据库是否被复制(您可以在copyDatabase之后在SD中看到它)吗?如果文件夹路径不存在而无法复制,因为你的onCreate为空,它将无法复制。