我有一个名为3列的人.他们是ID,NAME和PASSWORD.我需要添加一个名为AGE的新专栏.我试试这种方式但它对我不起作用请帮助我克服这个问题 public class Userdb { public static final String KEY_ROWID = "Id
public class Userdb { public static final String KEY_ROWID = "Id"; public static final String KEY_NAME = "Person_name"; public static final String KEY_PASSWORD = "Person_password"; public static final String KEY_AGE = "Person_age"; private static final String DATABASE_NAME = "People_db_1"; private static final String DATABASE_TABLE = "people_table"; private static final int DATABASE_VERSION = 1; private static class Dbhelper extends SQLiteOpenHelper { public Dbhelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME + " TEXT NOT NULL, " + KEY_PASSWORD + " TEXT NOT NULL); "); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE); db.execSQL("ALTER TABLE " + DATABASE_TABLE +" ADD COLUMN "+ KEY_AGE +" TEXT NOT NULL ; "); onCreate(db); } }数据库升级
SQLiteOpenHelper实现的构造函数应该调用超级构造函数,传递数据库名称和版本.仅当版本整数大于模拟器中运行的当前版本时,才会调用onUpgrade()方法.如果要调用onUpgrade()方法,则需要在代码中增加版本号.
关于onCreate()和onUpgrade()的See More