问题onUpdate android

时间:2011-09-09 19:56:11

标签: android database upgrade

我想在我的数据库中添加一个新表,而不是更改我已经拥有的其他两个表。因此,当用户升级到应用程序时,将保留创建的两个表,并将新表添加到数据库中。所以我这样做了:

private static class toDoDBOpenHelper extends SQLiteOpenHelper {
    public toDoDBOpenHelper(Context context, String name,
    CursorFactory factory, int version) {
    super(context, name, factory, version);
    }
    // SQL Statement to create a new database.
    private static final String DATABASE_CREATE = "create table " +
    DATABASE_TABLE_1 + " (" + KEY_ID1 + " integer primary key autoincrement, " +
    CLIENT_NAME + " text not null);";

    private static final String DATABASE_CREATE3 = "create table " +
     DATABASE_TABLE_3 + " (" + KEY_ID3 + " integer primary key autoincrement, " +
      DESCRIPTION + " text " + LIDER + " text " + GOAL + " text " +
       PROJECT_ID + " integer );";
    @Override
    public void onCreate(SQLiteDatabase _db) {
    _db.execSQL(DATABASE_CREATE);
    _db.execSQL("CREATE TABLE "+DATABASE_TABLE_2+" ("+ KEY_ID2 + " integer primary key autoincrement, "+PROJECT_NAME + " text not null, " + KEY_CREATION_DATE + " long, " +START_TIME +" text, "+
            START_DATE+ " text, " + END_TIME + " text, "+ END_DATE + " text, " +
            TOTAL + " text, "+ CLIENT_ID + " integer );");
    _db.execSQL("CREATE TABLE "+DATABASE_TABLE_3+" ("+ KEY_ID3 + " integer primary key autoincrement, "+DESCRIPTION + " text, " + LIDER + " text, " + GOAL +" text, "+
             PROJECT_ID + " integer );");
    }
    @Override
    public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) {


    onCreate(_db);
    }
    }

所以我基本上在onCreate方法中添加了另一个表,并在onUpgrade中调用了onCreate。但是现在应用程序在升级后第一次运行时崩溃了..用户遇到了这个错误:

java.lang.RuntimeException: Unable to start activity       
ComponentInfo{com.tracking.app/com.tracking.app.Main}: 
android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 
1 to 2: /data/data/com.tracking.app/databases/trackingApp.db
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
at android.app.ActivityThread.access$2300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:144)
at android.app.ActivityThread.main(ActivityThread.java:4937)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: Can't upgrade read-only database 
from version 1 to 2: /data/data/com.tracking.app/databases/trackingApp.db
at 
android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:170)
at com.tracking.app.ToDoDBAdapter.open(ToDoDBAdapter.java:84)
at com.tracking.app.Main.onCreate(Main.java:74)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
... 11 more

是否与我的open()函数有关?这是代码:

public void open() throws SQLiteException {
try {
db = dbHelper.getWritableDatabase();
} catch (SQLiteException ex) {
db = dbHelper.getReadableDatabase();
}
}

如果有人知道我做错了什么,请尽快告诉我。提前谢谢你

1 个答案:

答案 0 :(得分:0)

尝试使用此代码

public void open() throws SQLiteException 
{
    try
    { 
       db = dbHelper.getWritableDatabase(); 
    }
    catch (SQLiteException ex)
    {
    } 
}