2011-12-30 112 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,并且只能在创建或升级时添加/删除列。现在你想用这个完成什么?