我在我的应用程序中创建了一个表:
public static SQLiteDatabase attendancedb = this.openOrCreateDatabase("Attendance", Context.MODE_PRIVATE, null);
attendancedb.execSQL("CREATE TABLE IF NOT EXISTS attendance ( subName VARCHAR , attend VARCHAR, total VARCHAR, PRIMARY KEY (subName))");
如何在表格中再添加一列(注释)?
如果我这样做:
attendancedb.execSQL("CREATE TABLE IF NOT EXISTS attendance ( subName VARCHAR , attend VARCHAR, total VARCHAR,notes VARCHAR, PRIMARY KEY (subName))");
更新的应用程序在访问笔记数据时崩溃,但如果我重新安装该应用程序则可以工作。
或者现在和将来使用 Alter 查询是否有效?
我没有使用过这个SQLiteOpenHelper
类。
这是解决方案
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, "Attendance", null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
if(checktable(sqLiteDatabase)==0){ //for old user
try {
sqLiteDatabase.execSQL("ALTER TABLE attendance ADD notes VARCHAR;");
}catch (Exception e){ //for new user
sqLiteDatabase.execSQL("CREATE TABLE attendance ( subName VARCHAR , attend VARCHAR, total VARCHAR,notes VARCHAR, PRIMARY KEY (subName))");
}
}
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int old, int newVersion) {
}
int checktable(SQLiteDatabase db){
try{
Cursor c= db.rawQuery("SELECT notes FROM attendance", null);
return 1;
}catch (Exception e){
return 0;
}
}
}