我的代码:
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);
}
提前致谢
答案 0 :(得分:1)
由于您已经选择删除并重新创建表格,因此只需将新列规范添加到CREATE TABLE
中的onCreate()
。
增加DB_VESION
以便onUpgrade()
被调用。
答案 1 :(得分:0)
我的猜测是你正在扩展SQLiteOpenHelper类,以便能够管理你的数据库。
如果是这种情况,构造函数的最后一个参数是DB_VERSION。你应该增加这个数字,以便调用onUpgrade。但请注意,每次更改DB_VERSION时,都会调用onUpgrade方法,因此我建议使用开关来识别您需要执行的每个数据库升级。否则,您可能会发现自己尝试添加已存在的列。
如果您正在寻找添加列的代码,请尝试以下方法:
String createAlarmColum =“ALTER TABLE notes ADD COLUMN alarm INT;”;
db.execSQL(createAlarmColum);
希望有所帮助:)
答案 2 :(得分:0)
onUpgrade方法中的代码可能是这样的??
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { for (int i = oldVersion; i < newVersion; i++) { switch (i) { case 2: String createAlarmColum = "ALTER TABLE notes ADD COLUMN alarm INT;"; db.execSQL(createAlarmColum); ForgetLogManager.showAppLog(createAlarmColum); break; } } }// onCreate(db);