我有一个问题。
我的数据库中只有一个表
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL( "CREATE TABLE books (_id INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT, author TEXT,isbn TEXT,city TEXT);");
Log.v("onCreate-------","called onCreate");
}
这里: -
sqlite> select * from books;
也工作正常。
但我想添加新表,所以我想在这里添加: -
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
android.util.Log.w("books",
"Upgrading database, which will destroy all old data");
//db.execSQL("ALTER TABLE myallbooks ADD COLUMN city2");
db.execSQL("CREATE TABLE myfields (author TEXT,isbn TEXT,city TEXT);");
Log.v("onUpgrade-------","called onUpgrade with alter");
}
更改版本号后,我运行应用程序,但我没有在任何地方找到该表。
源码>从myfields中选择*;
Error:-no such table :myfield.
..................为什么不创造
答案 0 :(得分:2)
onUpgrade()
。看起来您没有增加版本并尝试选择myfields
表的列。
请检查您是否将版本号更新为更高的版本号。
答案 1 :(得分:1)
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL( "CREATE TABLE books (_id INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT, author TEXT,isbn TEXT,city TEXT);");
Log.v("onCreate-------","called onCreate");
db.execSQL("CREATE TABLE myfields (author TEXT,isbn TEXT,city TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
android.util.Log.w("books",
"Upgrading database, which will destroy all old data");
//db.execSQL("ALTER TABLE myallbooks ADD COLUMN city2");
// db.execSQL("CREATE TABLE myfields (author TEXT,isbn TEXT,city TEXT);");
//delete tables
db.execSQL("DROP TABLE IF EXISTS books");
// Create tables again`enter code here`
onCreate(db);
Log.v("onUpgrade-------","called onUpgrade with alter");
}