我试图通过 _id 检索一行数据,并使用我收到的数据创建接种的实例。
public Inoculation retrieveInoculationById(int id) {
Inoculation inoculation = new Inoculation();
Cursor cursor = getDataById(InoculationsDao.TABLE_NAME, InoculationsDao.ID, id);
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
inoculation.setBatchNumber(cursor.getString(cursor.getColumnIndex(InoculationsDao.BATCH_NUMBER)));
inoculation.setAdministrationSite(cursor.getString(cursor.getColumnIndex(InoculationsDao.ADMINISTRATION_SITE)));
inoculation.setAdministrationDate(cursor.getString(cursor.getColumnIndex(InoculationsDao.DATE)));
}
}
cursor.close();
return inoculation;
}
getDataById
方法很简单:
public Cursor getDataById(String TABLE_NAME, String ID, int id) {
Cursor cursor = databaseConnection.rawQuery("SELECT * FROM " + TABLE_NAME + "WHERE " + ID + " = " + id , null);
return cursor;
}
似乎在rawQuery
字符串中失败。
答案 0 :(得分:1)
这是因为您的rawQuery
字符串错误,因为您错过了空格。
其中:
Cursor cursor = databaseConnection.rawQuery("SELECT * FROM " + TABLE_NAME + "WHERE " + ID + " = " + id , null);
会产生一个查询:
"SELECT * FROM TABLE_NAMEWHERE ID = id"
应该是:
Cursor cursor = databaseConnection.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + ID + " = " + id , null);
使用rawQuery,您可以使用query()。像这样:
// Your table columns:
private String[] allColumns = { ID, YOUR_OTHER_COLUMN };
Cursor cursor = database.query(TABLE_NAME, allColumns, ID + " = " + id, null,
null, null, null);