gpt4 book ai didi

java - 我如何在 Android 中升级我的数据库

转载 作者:搜寻专家 更新时间:2023-10-30 23:48:03 25 4
gpt4 key购买 nike

你能帮帮我吗?我想在应用程序中升级我的数据库。我需要创建名为“alarm”的新列。我如何在 onUpgrade() 方法中做到这一点?p.s 在谷歌我没有找到需要的信息。以及如何将旧数据库中的信息保存到新数据库中

我的代码:

public static final String DB_NAME = "database.db";
public static final int DB_VESION = 1;
public static final String ID_COLUMN = "_id";
public static final String NOTE_COLUMN = "note";
public static final String DATE_COLUMN = "date_create";
public static final String STATE_COLUMN = "state";
public static final String DAY_STATE_COLUMN = "day_state";
public static final String ALARM_COLUMN = "alarm";
public final String[] allColumns = {ID_COLUMN, DATE_COLUMN, NOTE_COLUMN, STATE_COLUMN, DAY_STATE_COLUMN};
public static final String TABLE_NAME = "notes";

@Override
public void onCreate(SQLiteDatabase db) {
final String CREATE_DB = "CREATE TABLE " + TABLE_NAME + " (" +
ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT," +
DATE_COLUMN + " LONG NOT NULL," +
NOTE_COLUMN + " VARCHAR(140) NOT NULL," +
STATE_COLUMN + " INTEGER NOT NULL," +
DAY_STATE_COLUMN + " LONG" +
");";
db.execSQL(CREATE_DB);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
//how i can do this????
onCreate(db);
}

提前致谢

最佳答案

由于您已经选择删除并重新创建表,只需将新列规范添加到 onCreate() 中的 CREATE TABLE

增加您的 DB_VESION 以便调用 onUpgrade()

关于java - 我如何在 Android 中升级我的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25567932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com