Android SQLite数据库列不存在错误
我一直收到有关sql数据库的错误消息 新错误Android SQLite数据库列不存在错误,android,sqlite,Android,Sqlite,我一直收到有关sql数据库的错误消息 新错误 07-03 01:52:08.037:ERROR/AndroidRuntime(627):java.lang.RuntimeException:无法启动活动组件信息{com.fttech.books/com.fttech.books.viewBooks}:android.database.sqlite.SQLiteException:无此类列:作者:,编译时:从集合中选择书籍、作者、isbn、评级 07-03 00:15:04.807:INFO/Da
07-03 01:52:08.037:ERROR/AndroidRuntime(627):java.lang.RuntimeException:无法启动活动组件信息{com.fttech.books/com.fttech.books.viewBooks}:android.database.sqlite.SQLiteException:无此类列:作者:,编译时:从集合中选择书籍、作者、isbn、评级 07-03 00:15:04.807:INFO/Database(927):sqlite返回:错误代码=1,消息=no这样的列:book 现在我在新的模拟器上遇到了这个错误 07-03 00:33:27.887:INFO/Database(384):sqlite返回:错误代码=1,消息=no这样的表:集合 这是我使用的数据库代码
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;
}
}
啊,嗯,很明显,你是在创建表
数据库\u表
,而不是集合
。重构时,您一定错误地保留了引号:
"create table " + " DATABASE_TABLE " + " ("
我在你的数据库中没有看到作者栏,你的意思是:
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);";
您的代码看起来正常,您是否尝试过删除应用程序的数据?也许它有你创建的数据库的第一个实例,它没有这个列?是的,我的上一个数据库有这个列。它实际上拥有所有这些栏目。所以我现在在另一个模拟器上运行它,看看会发生什么。我刚刚编辑了关于问题,在顶部添加了我的新错误。我现在犯了这个错误你什么意思?我正在创建数据库表,而不是集合?Ohhhhhhnvm!我懂了!我刚修好。即将尝试它,看看它现在是否工作。它现在给我这个错误。。。07-03 01:48:11.096:INFO/Database(561):sqlite返回:错误代码=1,msg=没有这样的列:author07-03 01:52:08.037:error/AndroidRuntime(627):java.lang.RuntimeException:无法启动活动组件信息{com.fttech.books/com.fttech.books.viewBooks}:android.Database.sqlite.SQLiteException:没有这样的列:作者:,编译时:SELECT book、author、isbn、collectionauthor的评级也不在create语句中。
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);";