我有一个活动,可以将项目添加到创建的数据库中。我能够通过查询数据库来检索此添加的项目。我想知道为什么每次尝试检索与该行关联的主键整数时,我的应用程序都会崩溃?
这是我的数据库合约类。它实现了BaseColumns,它会自动为我创建一个_ID变量。
public static class FoodList implements BaseColumns {
public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon().appendPath(PATH).build();
public static final String TABLE_NAME = "food_table";
public static final String ITEM_NAME = "item";
}
这是我的数据库类:
public class FoodDatabase {
private SQLiteDatabase db;
private DatabaseHelper databaseHelper;
private Context context;
public static final int DB_VERSION = 2;
public static final String DB_NAME = "food_list_db";
private final String CREATE_DATABASE = "CREATE TABLE " + FoodContract.FoodList.TABLE_NAME + " ("
+ FoodContract.FoodList._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ FoodContract.FoodList.ITEM_NAME + " TEXT"
+ ");";
public FoodDatabase(Context context){
this.context = context;
databaseHelper = new DatabaseHelper(context);
}
public FoodDatabase openWriteableDB(){
db = databaseHelper.getWritableDatabase();
return this;
}
public FoodDatabase openReadableDB(){
db = databaseHelper.getReadableDatabase();
return this;
}
public long insertRow(ContentValues cv){
return db.insert(FoodContract.FoodList.TABLE_NAME,null,cv);
}
public Cursor getAllRows(String[] projection, String selection, String[] selectionArgs, String sortOrder){
return db.query(FoodContract.FoodList.TABLE_NAME,projection,selection,selectionArgs,null,null,sortOrder);
}
public long deleteRow(long id, String whereClause, String[]whereArgs){
return db.delete(FoodContract.FoodList.TABLE_NAME,whereClause,whereArgs);
}
public void closeDB(){
db.close();
}
private class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_DATABASE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + FoodContract.FoodList.TABLE_NAME);
Log.v ("DATABASE", "*********DATABASE DROPPED AND RECREATED*********");
onCreate(db);
}
}}
以下是查询语句在我的主片段中的样子。游标是从 ContentProvider 类中检索的...
try{
final Cursor query = getActivity().getContentResolver().query(FoodContract.FoodList.CONTENT_URI, null, null, null, null);
}catch (Exception e){
e.printStackTrace();
Log.v(TAG, "****************Failed to Open**************");
}
下面是导致应用程序在我的内容提供程序的查询方法中崩溃的语句。同样,我能够检索字符串,但不能检索长 id。有谁知道为什么?
while(cursor.moveToNext()) {
Long id = cursor.getLong(cursor.getColumnIndex(FoodContract.FoodList._ID));
String item = cursor.getString(cursor.getColumnIndex(FoodContract.FoodList.ITEM_NAME));
Log.v(TAG, "*********id = " + id + " ITEM = " + item + "************");
}
这是例外
Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.
您是否尝试过在cursor.MoveToNext()
之前致电cursor.moveToFirst()
?
问题出在这个声明中:
final Cursor query = getActivity().getContentResolver().query(FoodContract.FoodList.CONTENT_URI, null, null, null, null);
第二个参数不应为 null,而应为返回的游标应"查看"的列或"投影"。在这种情况下:
String[] projection = {FoodContract.FoodList._ID,FoodContract.FoodList.ITEM_NAME};
此 String[] 在内容提供程序中用于访问数据库。