fun readChore(id:Int):Chore{
val db:SQLiteDatabase=writableDatabase
val cursor:Cursor=db.query(TABLE_NAME,arrayOf(KEY_ID,KEY_CHORE_ASSIGNED_TO, KEY_CHORE_ASSIGNED_BY,
KEY_CHORE_ASSINED_TIME),KEY_ID+"=?",arrayOf(id.toString()),
null,null,null,null)
Log.d("position",cursor.position.toString())
cursor.moveToFirst()
val chore=Chore(
cursor.getString(cursor.getColumnIndex(KEY_CHORE_NAME)),
cursor.getString(cursor.getColumnIndex(KEY_CHORE_ASSIGNED_TO)),
cursor.getString(cursor.getColumnIndex(KEY_CHORE_ASSIGNED_BY)),
cursor.getLong(cursor.getColumnIndex(KEY_CHORE_ASSINED_TIME))
)
return chore
}
但我得到的只是一个错误_saying ... "无法从CursorWindow读取第0行第-1列,其中包含1行,4列。"
答案 0 :(得分:1)
您必须将KEY_CHORE_NAME
添加到所选行
val cursor:Cursor=db.query(TABLE_NAME,arrayOf(KEY_ID, KEY_CHORE_NAME ,KEY_CHORE_ASSIGNED_TO, KEY_CHORE_ASSIGNED_BY,
KEY_CHORE_ASSINED_TIME),KEY_ID+"=?",arrayOf(id.toString()),
null,null,null,null)
为了将来的调试,您可以使用Databaseutils.dumpCursor()
查看光标内容,否则很难看到。
也不要忘记在返回语句前关闭游标cursor.close()
。