我正在尝试使用SQLite
从我的数据库中检索单行但由于某种原因我的程序崩溃了。当我搜索日志时,我得到下一个错误:
请求索引-1,大小为1
我在网上搜索了解决方案,但看起来我的代码是正确的。我可以用该参数删除一行,所以我知道该位置是正确的。这可能是我编写查询的方式,但我不知道它有什么问题。谁能明白为什么我做错了? 这是查询的代码:
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + /jokes_table");
final Uri _URI = Uri.parse(MyContentProvider.CONTENT_URI + "/2");
String positions = intent.getStringExtra("position_in_db");
Cursor cur = getBaseContext().getContentResolver().query(_URI, new String[] {"Joke","Author","Date","Status"} , MyContentProvider.ID + " = " + intent.getStringExtra("position_in_db") , null , null );
我的查询方法:
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String a;
switch (sUriMatcher.match(uri))
{
case COLLECTION_URI_INDICATOR:
qb.setTables(TABLE_NAME);
qb.setProjectionMap(projectionMap);
break;
case SINGLE_ITEM_URI_INDICATOR:
qb.setTables(TABLE_NAME);
qb.setProjectionMap(projectionMap);
qb.appendWhere(ID);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, null);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
当我尝试通过该查询获取所有行时,它可以正常工作。唯一的问题是我无法检索特定的行。我尝试使用?
更改选择并尝试在调用查询之前查看字符串,但它不起作用。尝试通过
cur.getString(getColumnIndex("Joke"));
结束该计划。有人能帮帮我吗?
答案 0 :(得分:0)
你的selection
和selectionArgs
??
你可以试试这个
selection = ROW_ID_COLUMN_NAME + " =? "; // take a note on the "Space" between this statement
selectionArgs = { ROW_ID };
c = qb.query(db, projection, selection, selectionArgs, null, null, null);
// moveToFirst() method may prevent error when accessing 0 row cursor.
if (c.moveToFirst()) {
c.getString(getColumnIndex("Joke"));
}