-2

我想在我的书籍表中添加新列,但显示错误:- 我做错了什么。

引起:android.database.sqlite.SQLiteException:表书已经存在

以上错误

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

        }

        @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("DROP TABLE IF EXISTS books");
            db.execSQL("ALTER TABLE books ADD COLUMN city2 TEXT");

            onCreate(db);
            Log.v("onUpgrade-------","called onUpgrade with alter"); 
        }
4

3 回答 3

2
onCreate(db);
Log.v("onUpgrade-----","调用 onUpgrade with alter");

此处无需再次调用 onCreate。那就是问题所在。

于 2013-01-28T05:58:31.927 回答
0

将创建查询更改为 -

CREATE TABLE IF NOT EXISTS books....
于 2013-01-28T06:00:37.603 回答
0

使用以下检查数据库是否已经存在?
可能会帮助你。

public static final String DB_PATH = "/data/data/com.test.appname/databases/";
public static final String DB_NAME = "databasename.db";
public boolean checkDBExists()
{
    try 
    {
        File file = new File(DB_PATH+DB_NAME);
        if(file.exists())
        {
          // do nothing
        }
        else
        {
         db.execSQL( "CREATE TABLE books (id INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT ,author TEXT,isbn TEXT,city TEXT );");
        }
   }
   catch (Exception e) 
   {
     e.printStackTrace();
     return false;
   }
}
于 2013-01-28T06:18:36.490 回答