0

我想将我的数据库版本升级到 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)

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

4

2 回答 2

1

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

在这种情况下,它将简单地通过 if 循环并进入 final else,它试图删除 table moduleDesc,只有在用户计划从版本 1 或 2 升级到版本 3 时才会创建该表。

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

于 2012-07-31T06:45:56.773 回答
0

您无需更改 mainfest 文件,您可以更改您的 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);
}

  }
于 2012-07-31T06:44:25.743 回答