我有一个Activity,它将一个项添加到创建的数据库中。我可以通过查询数据库来检索这个添加的项目。我想知道为什么,每当我尝试检索与该行关联的主键整数时,我的应用程序崩溃了?
这是我的DB合同类。它实现了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.
答案 0 :(得分:0)
您是否曾尝试在.delete_button
之前致电cursor.moveToFirst()
?
答案 1 :(得分:0)
问题出在本声明中:
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="container"></div>
第二个参数不应为null,而是列或&#34;投影&#34;返回的光标应该是&#34;看着。&#34;在这种情况下:
final Cursor query = getActivity().getContentResolver().query(FoodContract.FoodList.CONTENT_URI, null, null, null, null);
在ContentProvider中使用此String []来访问数据库。