由于此类异常,我的活动会继续强制关闭
07-02 01:24:59.244: ERROR/AndroidRuntime(660):
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fttech.collection/com.fttech.collection.book_list}:
java.lang.IllegalArgumentException: column '_id' does not exist
这是我的班级
public class bookDbHelper{
private static final String DATABASE_NAME = "data";
private static final String DATABASE_TABLE = "books";
private static final int DATABASE_VERSION = 1;
public static final String KEY_BOOKTITLE = "title";
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_BOOKTITLE + " text not null, "
+ KEY_AUTHOR + " text not null, "
+ KEY_ISBN + " text not null, "
+ KEY_RATING + " text not null);";
private final Context mCtx;
public bookDbHelper(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 arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
public bookDbHelper open()throws SQLException{
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close(){
mDbHelper.close();
}
public long addBook(String book_name, String author, String isbn, String rating){
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_BOOKTITLE, book_name);
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_BOOKTITLE, KEY_AUTHOR, KEY_ISBN, KEY_RATING, KEY_ROWID}, null, null, null, null, null);
}
public Cursor fetchBook(long rowId)throws SQLException {
Cursor mCursor =
mDb.query(true, DATABASE_TABLE, new String[] {KEY_BOOKTITLE, KEY_AUTHOR, KEY_RATING, KEY_ISBN, KEY_ROWID}, KEY_ROWID + "=" + rowId, null, null, null, null, null);
if(mCursor != null){
mCursor.moveToFirst();
}
return mCursor;
}
public boolean updateBooks(long rowId, String book_name, String author, String rating, String isbn){
ContentValues args = new ContentValues();
args.put(KEY_BOOKTITLE, book_name);
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;
}
}
这是我的第二个类,它从SQLbase
public class book_list extends ListActivity{
private static final int ACTIVITY_CREATE = 0;
private static final int ACTIVTY_EDIT = 1;
private bookDbHelper mDbHelper;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.book_list);
mDbHelper = new bookDbHelper(this);
mDbHelper.open();
fillData();
registerForContextMenu(getListView());
}
private void fillData() {
Cursor booksCursor = mDbHelper.fetchAllBooks();
startManagingCursor(booksCursor);
String [] from = new String[]{bookDbHelper.KEY_BOOKTITLE};
int[] to = new int[]{R.id.text1};
SimpleCursorAdapter books =
new SimpleCursorAdapter(this, R.layout.book_row, booksCursor, from, to);
setListAdapter(books);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
Intent i = new Intent(this, book_edit.class);
i.putExtra(bookDbHelper.KEY_ROWID, id);
startActivityForResult(i, ACTIVTY_EDIT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
fillData();
}
}
public class book_list extends ListActivity{
private static final int ACTIVITY_CREATE = 0;
private static final int ACTIVTY_EDIT = 1;
private bookDbHelper mDbHelper;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.book_list);
mDbHelper = new bookDbHelper(this);
mDbHelper.open();
fillData();
registerForContextMenu(getListView());
}
private void fillData() {
Cursor booksCursor = mDbHelper.fetchAllBooks();
startManagingCursor(booksCursor);
String [] from = new String[]{bookDbHelper.KEY_BOOKTITLE};
int[] to = new int[]{R.id.text1};
SimpleCursorAdapter books =
new SimpleCursorAdapter(this, R.layout.book_row, booksCursor, from, to);
setListAdapter(books);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
Intent i = new Intent(this, book_edit.class);
i.putExtra(bookDbHelper.KEY_ROWID, id);
startActivityForResult(i, ACTIVTY_EDIT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
fillData();
}
}
答案 0 :(得分:0)
也许
public static final String KEY_ROWID = "_ID";
必须是小写
public static final String KEY_ROWID = "_id";
答案 1 :(得分:0)
我建议你将数据库文件下载到你的电脑上:
adb pull /data/data/[your.application.package.name]/databases/[your.database.name] .
然后使用SQLite Manager之类的东西来查看数据库本身。数据库可能是使用某些早期版本的代码创建的,并且从未使用正确的列进行更新,例如......