"No Such Table" Error found in SQLite Android

Viewed 55528

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

11 Answers

Nothing about this made any sense, as the table existed from the very beginning of the DB, so we tried different things:

  1. uninstalling app

  2. claning cache / data

  3. changing database version number

  4. forcing to copy the database even if it already existed

Make sure that onCreate method called. I am also facing similar type of problem when creating multiple table. If you create a separate class make sure you first clear all the storage data of your app and then again run it ..It will work fine for me.

Also had the issue where I already had the app built with an existing DB, but wanted to add another table.

Besides having the onUpgrade method, I also had to increment the private static final Integer DB_VERSION, which is used when instantiating the DatabaseHelper, like so:

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

Also from the official documentation, my onUpgrade method looks like this:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // on upgrade drop older tables
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_1);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_2);

    // create new tables
    onCreate(db);
}

Then, after building the app, the new version of the database worked.

Note:

External Database You Access Time must add for the path in Sqliteopenhelper class

public final static String DATABASE_PATH = "/data/data/com.example.shortcuts/databases/";

com.example.shortcuts=Your Package name

Related