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"); 

    }

这里:-

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"); 
    }

更改版本号后,我运行该应用程序,但在任何地方都没有找到该表。

sqlite> 从 myfields 中选择 *;

Error:-no such table :myfield......................为什么它不创建

4

2 回答 2

2

onUpgrade()当您将数据库版本更改为更高版本时将调用。看起来您没有增加版本并试图选择表格的列myfields

请检查您是否将版本号更新为比早期版本更高的版本号。

于 2013-01-18T10:55:58.773 回答
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"); 
    }
于 2013-01-18T11:01:22.183 回答