简体   繁体   English

如何在我已经创建的表中添加新列?

[英]How to add a new column in my already created table?

I want to add a new column (item_price) in the already created table.I've tried some solutions using the onUpgrade method but still couldn't get it working.At first I literally added the column like a dummy then I saw posts that show how to use the onUpgrade method.我想在已经创建的表中添加一个新列(item_price)。我尝试了一些使用 onUpgrade 方法的解决方案,但仍然无法使其正常工作。起初我像虚拟一样添加了该列,然后我看到了帖子展示如何使用 onUpgrade 方法。

Here is my Constants class:这是我的常量 class:

public class Constants {
public static final int DB_VERSION = 2;
public static  final String DB_NAME = "babyList";
public static final String TABLE_NAME = "baby_tbl";

public static final String KEY_ID = "id";
public static final String KEY_BABY_ITEM = "baby_item";
public static final String KEY_QTY_NUMBER = "quantity_number";
public static final String KEY_COLOR = "color";
public static final String KEY_ITEM_SIZE = "size";
public static final String KEY_DATE_NAME = "date_added";
}

Here is part of my DataBaseHandlar class:这是我的 DataBaseHandlar class 的一部分:

public class DatabaseHandlar extends SQLiteOpenHelper {

private final Context context;

public DatabaseHandlar(@Nullable Context context) {
    super(context, Constants.DB_NAME, null, Constants.DB_VERSION);
    this.context = context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_BABY_TABLE = "CREATE TABLE " + Constants.TABLE_NAME + "("
            + Constants.KEY_ID + " INTEGER PRIMARY KEY,"
            + Constants.KEY_BABY_ITEM + " INTEGER,"
            + Constants.KEY_COLOR + " TEXT,"
            + Constants.KEY_QTY_NUMBER + " INTEGER,"
            + Constants.KEY_ITEM_SIZE + " INTEGER,"
            + Constants.KEY_DATE_NAME + " LONG);";

    db.execSQL(CREATE_BABY_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS " + Constants.TABLE_NAME);

    onCreate(db);
}

// CRUD operations
public void addItem(Item item) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(Constants.KEY_BABY_ITEM, item.getItemName());
    values.put(Constants.KEY_COLOR, item.getItemColor());
    values.put(Constants.KEY_QTY_NUMBER, item.getItemQuantity());
    values.put(Constants.KEY_ITEM_SIZE, item.getItemSize());
    values.put(Constants.KEY_DATE_NAME, java.lang.System.currentTimeMillis());//timestamp of the system

    //Inset the row
    db.insert(Constants.TABLE_NAME, null, values);

    Log.d("DBHandler", "added Item: ");
}

You can use the ALTER TABLE statement, this is used to add, delete, or modify columns in an existing table.您可以使用ALTER TABLE语句,该语句用于添加、删除或修改现有表中的列。

For example:例如:

ALTER TABLE table_name
ADD column_name datatype;

ALTER TABLE is what you are looking for ALTER TABLE 是您正在寻找的

ALTER TABLE {tableName} ADD COLUMN {columnName} {type};

You would need to manually alter the table with您需要手动更改表格

if(oldVersion == 1 && newVersion ==2){
    try {
         db.execSQL("ALTER TABLE " + Constants.TABLE_NAME + " ADD COLUMN mobilenumber TEXT DEFAULT ''");
        } catch (Exception ignored) {
            recreateDb(db);
        }
} else {
    onCreate(db);
}

put that in the onUpgrade method把它放在 onUpgrade 方法中

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

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