2011-12-30 55 views
2

如何升級只讀SQLite數據庫SQLiteException:不能只讀從版本X數據庫升級爲Y

我曾試圖改變SQLiteDatabase.OPEN_READWRITE);getReadableDatabase(); 結果還是一樣。


當我想從版本1升級我的SQLite datatbase 2,我得到這個錯誤:

SQLiteException: Can't upgrade read-only database from version X to Y

在第1 verison我的應用程序中,我用這個代碼:

private static final String DB_NAME = "this_the_database"; 
private final Context context; 
private static final int DATABASE_VERSION = 1; 

public DatabaseHelper(Context context) { 
    super(context, DB_NAME, null, DATABASE_VERSION); 
    this.context = context; 

    String path = context.getDatabasePath(DB_NAME).getAbsolutePath(); 
    SQLiteDatabase db = null; 
    try { 
     db = SQLiteDatabase.openDatabase(path, null, 
       SQLiteDatabase.OPEN_READONLY); 
    } catch (SQLiteException e) { // DB not found  
     db = getReadableDatabase(); 
     db.close(); 
     // Copy DB from asset folder. 
     copyDatabase(path); 
    } 

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

當我決定以此來upgarde數據庫,墜毀

@Override 
public void onCreate(SQLiteDatabase db) { 
    new DatabaseHelper(context).getReadableDatabase(); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS " + DB_NAME); 
    onCreate(db); 
} 

編號:http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

+0

請發佈完整的例外日誌 – Jack 2011-12-30 15:46:32

回答

1

爲何使用SQLiteDatabase.openDatabase?在DatabaseHelper構造函數中只需調用super(..)就足夠了。 getReadableDatabase()可以在open()方法中,也可以由打開數據庫的活動調用。 在apidemos中,有一個LoaderThrottle示例演示如何使用Android SQLite數據庫。

0

你不能在創建時獲得可讀的數據庫,但即使可以,數據庫已經傳入(SQLiteDatabase數據庫),並且當時沒有內容可讀。只有在應用程序啓動時首次創建數據庫時纔會調用create,並且只能在創建或升級時添加/刪除列。現在你想用這個完成什麼?

相關問題