tj walker
tj walker

Reputation: 1343

SQLite database column does not exist error

I keep getting this error about my sql database

NEW ERROR

07-03 01:52:08.037: ERROR/AndroidRuntime(627): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fttech.books/com.fttech.books.viewBooks}: android.database.sqlite.SQLiteException: no such column: author: , while compiling: SELECT book, author, isbn, rating FROM collection

07-03 00:15:04.807: INFO/Database(927): sqlite returned: error code = 1, msg = no such column: book

NOW I am getting this error on the new emulator

07-03 00:33:27.887: INFO/Database(384): sqlite returned: error code = 1, msg = no such table: collection

Here is my database code I used...

public class booksDbAdapter {

    private static final String DATABASE_NAME = "collection";
    private static final String DATABASE_TABLE = "collection";
    private static final int DATABASE_VERSION = 1;

    public static final String KEY_BOOK = "book";
    public static final String KEY_AUTHOR = "author";
    public static final String KEY_ISBN = "isbn";
    public static final String KEY_RATING = "rating";
    public static final String KEY_ROWID = "_id";

    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    private static final String DATABASE_CREATE =
                    " create table " + " DATABASE_TABLE " + " ("
                    + KEY_ROWID + " integer primary key autoincrement,      "
                    + KEY_BOOK + " text not null, "
                    + KEY_ISBN + " text not null, "
                    + KEY_RATING + " text not null);";

    private final Context mCtx;

    public booksDbAdapter (Context ctx){
        this.mCtx = ctx;
    }

    private static class DatabaseHelper extends SQLiteOpenHelper{
        DatabaseHelper(Context context){
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
        }
    }
    public booksDbAdapter open() throws SQLException{
        mDbHelper =  new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }
    public void close(){
        mDbHelper.close();
    }

    public long createBook(String book, String author, String isbn, String rating){
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_BOOK, book);
        initialValues.put(KEY_AUTHOR, author);
        initialValues.put(KEY_ISBN, isbn);
        initialValues.put(KEY_RATING, rating);

        return mDb.insert(DATABASE_TABLE, null, initialValues);
    }
    public boolean deleteBook(long rowId){
        return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }
    public Cursor fetchAllBooks(){
        return mDb.query(DATABASE_TABLE, new String[]{KEY_BOOK, KEY_AUTHOR, KEY_ISBN, KEY_RATING}, null, null, null, null, null);
    }
    public Cursor fetchBook(long rowId) throws SQLException{
        Cursor mCursor =
        mDb.query(DATABASE_TABLE, new String[]{KEY_BOOK, KEY_AUTHOR, KEY_ISBN, KEY_RATING}, KEY_ROWID + "=" +
                                rowId, null, null, null, null);

        if(mCursor != null){
            mCursor.moveToFirst();
        }
        return mCursor;

    }
    public boolean updateBook(long rowId, String book, String author, String isbn, String rating){
        ContentValues args = new ContentValues();
        args.put(KEY_BOOK, book);
        args.put(KEY_AUTHOR, author);
        args.put(KEY_ISBN, isbn);
        args.put(KEY_RATING, rating);
        return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null)> 0;
    }
}

Upvotes: 0

Views: 5455

Answers (2)

VMcPherron
VMcPherron

Reputation: 624

private static final String DATABASE_CREATE =
                " create table " + " DATABASE_TABLE " + " ("
                + KEY_ROWID + " integer primary key autoincrement,      "
                + KEY_BOOK + " text not null, "
                + KEY_ISBN + " text not null, "
                + KEY_RATING + " text not null);";

I don't see the Author column in your database, wouldn't you mean:

private static final String DATABASE_CREATE =
                " create table " + " DATABASE_TABLE " + " ("
                + KEY_ROWID + " integer primary key autoincrement,      "
                + KEY_BOOK + " text not null, "
                + KEY_ISBN + " text not null, "
                + KEY_AUTHOR + " text not null, "
                + KEY_RATING + " text not null);";

Upvotes: 0

dmon
dmon

Reputation: 30168

Ah, duh, it was so obvious, you're creating the table DATABASE_TABLE, not collection. You must have kept the quotes by mistake when refactoring:

     "create table " + " DATABASE_TABLE " + " ("

Upvotes: 2

Related Questions