public ArrayList < PatientInfo > getAllPatients(String username) {
ArrayList < PatientInfo > patients = new ArrayList < PatientInfo > ();
open();
Cursor cursor = db.query(UserTable, null, null, null, null, null, AccessedDate);
while (cursor != null && cursor.moveToNext()) {
try {
} catch (Exception ex) {
//Log.e("XXX", ex.getMessage());
}
}
if (cursor != null)
cursor.close();
close();
Collections.reverse(patients);
return patients;
}
在方法中将用户名作为参数,接下来怎么实现查询并且获取user specific result?
为了获取特定用户的结果,你需要指定一个条件来筛选出特定用户的行。您可以在查询语句中添加一个 WHERE 子句来筛选出所有符合条件的行,并将该子句中的列名替换为 username 列,并将条件设置为 username = ?。然后,您可以使用提供的 username 参数的值作为第二个参数传递给 query() 方法,以替换 ? 占位符。
例如,可以使用以下代码来获取特定用户的所有病人信息:
Cursor cursor = db.query(UserTable, null, "username = ?", new String[] {username}, null, null, AccessedDate);
然后,您可以使用与您之前一样的代码来循环游标,并将获取的数据添加到 patients 列表中。
请注意,您还可以使用其他方法,例如 rawQuery(),在 SQL 语句中编写查询并使用占位符。例如:
Cursor cursor = db.rawQuery("SELECT * FROM " + UserTable + " WHERE username = ?", new String[] {username});
请参阅 SQLite 的文档以获取有关使用 SQL 语句查询数据库的更多信息。