简体   繁体   English

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

[英]How to upgrade database version in android?

I want to upgrade my database version to 4. I have changed version code in manifest file and my database openhelper is 我想将数据库版本升级到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);
        }
    }

The error that I get is 我得到的错误是

     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)

can anyone tell me how to upgrade database 谁能告诉我如何升级数据库

I believe your issue could be the if statement loop that you have. 我相信您的问题可能是您拥有的if语句循环。 What if the user has either version 1 or version 2 of your application and wants to upgrade to the latest one, which I believe is 4. 如果用户拥有应用程序的版本1或版本2,并且想要升级到最新版本(我认为是4),该怎么办?

In that case, it will simply fall through the if loop and go to the final else , which tries to drop the table moduleDesc , which only gets created if the user had planned to upgrade from either version 1 or 2 to version 3. 在这种情况下,它将简单地通过if循环进入最后的else ,这将尝试删除表moduleDesc ,该表只有在用户计划从版本1或版本2升级到版本3时才创建。

So in summary, examine your if statements and cater to all conditions. 因此,总而言之,请检查您的if语句并满足所有条件。

you no need to change in mainfest file u can change in ur sql data base class..check the below code,u have to change in DATABASE_VERSION =7; 您无需更改mainfest文件即可更改ur 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);
}

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM