如何将新列添加到表中?我有一个名为的表Subject
和一个名为 的列module
。现在我想在表中添加名为and 的question
新列。例子:answer
Subject
表:主题
模块:数学
问题:2x2
答案:4
数据库助手:
public class DBHelper extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "Voting";
// Set table name
static final String TABLE_NAME = "Subject";
// Set Table Columns
private static final String KEY_ID = "id";
static final String KEY_MODULE = "module";
static final String KEY_QUESTION = "question";
static final String KEY_ANSWER = "answer";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
// Category table create query
String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_MODULE + " TEXT" + KEY_QUESTION + "TEXT" + KEY_ANSWER + "TEXT)";
db.execSQL(CREATE_CATEGORIES_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// Create tables again
onCreate(db);
}
/**
* Inserting new Column into subjects table
* */
public void insertColumn(String column){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_MODULE, column);
// Inserting Row
db.insert(TABLE_NAME, null, values);
db.close(); // Closing database connection
}
如果我的代码在答案中更新以便我可以轻松理解,这将很有帮助。