如何在android中升级数据库版本?

时间:2012-07-31 06:35:47

标签: android sqlite

我想将我的数据库版本升级到4。 我在清单文件中更改了版本代码 我的数据库openhelper是

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        DTLog.w("Old Version" + oldVersion, "new Version" + newVersion);
        DTLog.w("UPDATING DB", "Updating database");

        if (oldVersion == 1 && newVersion == 2) {
            db.execSQL("ALTER TABLE moduleRecord ADD COLUMN subtitle1 TEXT");
            db.execSQL("ALTER TABLE moduleRecord ADD COLUMN subtitle2 TEXT");
            db.delete("moduleRecord", null, null);

        } 
        else if ((oldVersion == 2&&newVersion == 3)) {
            db.execSQL("CREATE TABLE "
                    + "moduleDesc"
                    + "(deleteable INTEGER,createable INTEGER,updateable INTEGER,name TEXT,label TEXT,labelfields TEXT,fields TEXT);");

        } else {
            db.execSQL("DROP TABLE IF EXISTS " + "moduleRecord");
            db.execSQL("DROP TABLE IF EXISTS " + "ModuleName");
            db.execSQL("DROP TABLE IF EXISTS " + "EventStatus");
            db.execSQL("DROP TABLE IF EXISTS " + "moduleDesc");
            onCreate(db);
        }
    }

我得到的错误是

     07-31 12:01:15.267: W/System.err(632): 
android.database.sqlite.SQLiteException: no such table: moduleDesc: ,
while compiling: insert into moduleDesc(deleteable,createable,updateable,name,label,labelfields,fields)
values(?,?,?,?,?,?,?)
    07-31 12:01:15.267: W/System.err(632):  
at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
    07-31 12:01:15.267: W/System.err(632): 

at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
    07-31 12:01:15.267: W/System.err(632): 

at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
    07-31 12:01:15.267: W/System.err(632): 

at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)

谁能告诉我如何升级数据库

2 个答案:

答案 0 :(得分:1)

我相信您的问题可能是您拥有的if语句循环。如果用户拥有您的应用程序的版本1或版本2并想要升级到最新版本,我认为该版本为4,该怎么办。

在这种情况下,它将简单地通过if循环并转到最后的else,它试图删除表moduleDesc,只有在用户计划从版本1或版本3。

总而言之,检查你的if语句并满足所有条件。

答案 1 :(得分:0)

你无需更改清单文件你可以更改你的sql数据库类..检查下面的代码,你必须在DATABASE_VERSION = 7中更改;

 public static final String TABLE_COMMENTS = "contacts";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_COMMENT = "account";
public static final String COLUMN_USER_ID = "contactJids";
public static final String COLUMN_USER_NAME = "contactNames";

private static final String DATABASE_NAME = "contacts.db";
private static final int DATABASE_VERSION =7;

// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
        + TABLE_COMMENTS + "( " + COLUMN_ID     + " integer primary key autoincrement, "
                                + COLUMN_COMMENT + " text not null, " 
                                + COLUMN_USER_ID + " text not null, "
                                + COLUMN_USER_NAME + " text not null );";

public MySQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
    database.execSQL(DATABASE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(MySQLiteHelper.class.getName(),
            "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS);
    onCreate(db);
}

  }