07-03 01:52:08.037: ERROR / AndroidRuntime(627): java.lang.RuntimeException:无法 开始活动 ComponentInfo {com.fttech.books / com.fttech.books.viewBooks}: android.database.sqlite.SQLiteException: 没有这样的专栏:作者:,而 编译:SELECT book,author,isbn, 评级来自收藏
代码:
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;
}
}
答案 0 :(得分:0)
我建议您通过命令行运行.dump sqlite数据库并检查它。然后通过命令行运行查询。如果一切正常,请寻找拼写错误。找出哪个方法有错误的查询也很有帮助,这样你就可以缩小问题范围。
答案 1 :(得分:0)
你的表真的有专栏作者吗?我假设您在通过db.execSQL(DATABASE_CREATE);
你的问题是你在onUpgrade()
内没有任何代码所以当你添加一个新列时它没有被添加,因为数据库创建会导致类似“数据库已经存在”的错误
您需要向onUpgrade()添加代码,这将至少删除所有表,然后重新创建。或者更好地运行转换表等......
然后你需要递增int DATABASE_VERSION
所以onUpgrade()会运行。最后,您的新专栏“作者”将被添加到表格中。
当然,您也可以通过command line
连接到数据库来检查列是否存在希望有所帮助
以下是我使用的一些代码,可能有助于创建/升级。显然我的表创建常量是我特有的......
编辑:...你也没有尝试/捕捉你的表创建。我确定如果添加这个,你会发现表create命令没有运行,因为它的表“已经存在等等......”
...
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);
}
...