开发者

I keep getting the SQLException error with this

开发者 https://www.devze.com 2023-03-16 19:05 出处:网络
07-03 01:52:08.037: ERROR/AndroidRuntime(627): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fttech.books/com.fttech.books.viewBooks}:

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

CODE:

public class booksDbAdapter {


private static final String DATABASE_NAME = " data";
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_AUTHOR + " text not null, "
        + 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;


        }

}


I suggest you run .dump the sqlite database via the command line and inspect it. Then run your queries through the command line as well. If all that works, look for typos. It would also be helpful to figure out which method has the bad query so you can narrow the problem down.


Does your table really have the column author? i assume you've added that column after creating the table via db.execSQL(DATABASE_CREATE);

your problem is you don't have any code within onUpgrade() so when you add a new column its not added because database create will result in an error something like "database already exists"

You need to add code to onUpgrade() which will atleast drop all tables then recreate. or better run an alter table etc...

you then need to increment int DATABASE_VERSION so onUpgrade() will run. FINALLY your new column "author" will be added to the table..

of course you can also check if the column exists by connecting to your database via the command line

hope that helps

Here is some code i use which might help regarding creation/upgrade. Obviously my table create constants are specific to me...

Edit: ...also you have no try/catch around your table create. I am sure if add this, you'll discover the table create command is not running as its getting "table already exists etc..."

...
DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    try {
        db.execSQL(CREATE_TABLE_SITE);  
        db.execSQL(CREATE_TABLE_SITE_USER);
        db.execSQL(CREATE_TABLE_ARTICLE);
        db.execSQL(CREATE_TABLE_USER);
    } catch (Exception e) {
        Log.e("dbAdapter", e.getMessage().toString());
    }
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
            + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS user");
    db.execSQL("DROP TABLE IF EXISTS site");
    db.execSQL("DROP TABLE IF EXISTS site_user");
    db.execSQL("DROP TABLE IF EXISTS article");
    onCreate(db); 
}
...
0

精彩评论

暂无评论...
验证码 换一张
取 消