Android的SQLite的 - [否这样的表"错误错误、Android、SQLite、QUOT

2023-09-14 23:05:21 作者:迁就-

我想了解SQLite数据库,但是我真的很讨厌处理任何后端的东西,与激情。我已经打墙用一个看似简单的问题。

I am trying to learn about Sqlite databases, but I really hate dealing with any back-end stuff, with a passion. I'm already hitting walls with a seemingly simple problem.

下面是code,我觉得从DatabaseHelper类事项

Here is the code that I think matters from the DatabaseHelper class

public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "Library";

public static final String TABLE_NAME = "books";
public static final String TITLE = "title";
public static final String AUTHOR = "author";
public static final String ISBN = "isbn";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, author TEXT, isbn TEXT)");
}


public boolean insertBook(String title, String author, String isdn) {

    try {
        SQLiteDatabase db = getWritableDatabase();

        ContentValues cv = new ContentValues();
        cv.put(TITLE, title);
        cv.put(AUTHOR, author);
        cv.put(ISBN, isdn);

        db.insert(TABLE_NAME, null, cv);
        db.close();

        return true;
    } catch (Exception exp) {
        exp.printStackTrace();

        return false;
    }
}

}

这是code在我的主要活动

And this is the code in my main activity

    dbHelper = new DatabaseHelper(this);

    dbHelper.insertBook("Harry Potter", "JK", "1000");
    dbHelper.insertBook("Hamlet", "Shakespeare", "500");

Eclipse的是告诉我,有一个在insertBook()方法的错误。它说,有没有这样的表书:......。我不知道我在做什么错在这里。是什么使得它更令人沮丧的是,只有一两分钟,然后才可以正常使用,然后(我认为)我的表中删除,它只是重新创建它无论出于何种原因,尽管这code没有,因为我改变了先创建它(我想...)。

Eclipse is telling me that there is an error in the insertBook() method. It says that there is "no such table books: ...". I have no idea what I am doing wrong here. What makes it more frustrating is that only a couple of minutes before it was working perfectly, then (I think) I dropped the table and it just create it again for whatever reason, even though this code has not changed since I first created it (I think...).

任何帮助将是很大的AP preciated。

Any help would be greatly appreciated.

推荐答案

还有一个老版本的设备上的数据库,里面确实有到位的(空)数据库,而不是书表。如果这是你的一个选择,只需卸载并重新安装应用程序。

There is an older version of the database on your device, which does have the (empty) database in place, but not the books table. If that's an option for you, just uninstall and reinstall the app.

后来,当你想要一个新的表添加到数据库中生产的最终用户设备中,但保留现有的数据,在指定的钩子添加新表,改变模式或升级您的数据是 onUpgrade 的方法,你的 SQLiteOpenHelper

Later, when you'd like to add a new table to the database during production on end-user devices, but keep existing data, the designated hook to add new tables, alter the schema or upgrade your data is the onUpgrade method of your SQLiteOpenHelper.