0

我试图从我的数据库中检索一行,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"));

结束程序。有人能帮助我吗?

4

1 回答 1

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"));
}
于 2012-09-02T12:12:01.327 回答