【问题标题】:SQLiteDatabaseCorruptException after upgrading database升级数据库后出现 SQLiteDatabaseCorruptException
【发布时间】:2013-01-12 13:18:44
【问题描述】:

我已经升级了我的数据库,它可以在一些手机上完美运行。但是,在其他情况下,我得到 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 方法是否有错误?

【问题讨论】:

标签: android android-sqlite


【解决方案1】:

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

【讨论】:

  • 数据库被复制。升级后第一次出现错误。下次我打开应用程序时,它运行良好。
猜你喜欢
  • 2020-10-13
  • 1970-01-01
  • 2014-02-11
  • 2018-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-10
相关资源
最近更新 更多