开发者

problem onUpdate android

开发者 https://www.devze.com 2023-04-04 01:07 出处:网络
I wanted to add a new table in my database, and not change the other two that I already had. So when the user will make an upgrade to the app, the two tables that were created would remain and a new t

I wanted to add a new table in my database, and not change the other two that I already had. So when the user will make an upgrade to the app, the two tables that were created would remain and a new table would be added to the database. So I did this:

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);
    }
    }

So I basically added the another table in the onCreate method and I called onCreate in onUpgrade. But now the app crashes on the first run after the upgrading.. The users got this error:

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$MethodAndArgsCal开发者_运维问答ler.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

Does it have sth to do with my open() function? This is the code:

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

If someone has any idea what am i doing wrong please let me know as soon as possible. Thank u in advance


try just for this code

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号