我立即想打开这个答案是基于您正确使用 SQL 术语的事实,并且您知道 select two columns 意味着从返回的一个(或多个)行中选择两列一个查询
您使用了错误的WHERE 子句。它旨在选择列具有给定值的位置。例如,如果您有一个带有INTEGER PRIMARY_KEY 的表并且想要选择带有id = 10 的行,您可以使用SELECT * FROM table WHERE id=10。
您应该使用存储过程,以便转换为支持 SELECT * FROM table WHERE id=? 并传递一个 String[] 数组和要填充的参数。
现在您已经了解了 WHERE 子句的实际基础知识,让我继续您的代码。
您正在使用,,因此在字符串之外添加了更多参数。其次,你不能有多个传递的参数,唯一支持的是一个字符串列表,这是为了避免 SQL 注入。
现在,选择特定的列更容易了。
首先,您需要moveToFirst 以确保查询不为空。然后isAfterLast 用于检查是否没有更多条目。然后你只需选择你想要的适用值。示例:
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
//These are just examples of values you can select from the returned row. Edit based on your database and the rows you want. `COLUMN_ID` and `COLUMN_STACKTRACE`
//are Strings with the column name
int id = cursor.getInt(cursor.getColumnIndex(COLUMN_ID));
String stack = cursor.getString(cursor.getColumnIndex(COLUMN_STACKTRACE));
//Here you'd add these to a List or something, maybe as a dataclass. Essentially, TODO: Handle your data here
cursor.moveToNext();//Move to the next row
}
}
cursor.close();//Close the cursor
}
对WHERE 关键字解释的补充。
如果您有一行 id = 10, field1 = "11.12.17", field3 = "some data here" 并且您想根据 ID 选择该行,您可以使用 SELECT * FROM table WHERE id=10 选择 ID 为 10 的所有行。在某些情况下,您会得到一个行,在其他人中您可以获得 1000。