Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
889 views
in Technique[技术] by (71.8m points)

java - "No Such Table" Error found in SQLite Android

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.

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

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 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...).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...