当值为"完成时,我尝试在我的数据库中获取所有对象"但我得到一个例外。我认为我的查询是正确的,因为我在另一种方法中使用相同的查询来仅恢复对象。
populate
我的例外:
public List<DownloadProfile> getAllDownloadProfile(String state) {
List<DownloadProfile> downloadProfiles = new ArrayList<>();
Cursor cursor = db.rawQuery("SELECT * FROM " + DownloadEntry.TABLE_NAME
+ " WHERE " + DownloadEntry.COLUMN_NAME_DL_STATE + " = " + state + "", null);
if (cursor.getCount() > 0) {
cursor.moveToFirst();
do {
DownloadProfile downloadProfile = new DownloadProfile(-1, "", "", "");
downloadProfile.path = cursor.getString(cursor.getColumnIndex(DownloadEntry.COLUMN_NAME_DL_PATH));
downloadProfile.extension = cursor.getString(cursor.getColumnIndex(DownloadEntry.COLUMN_NAME_DL_EXTENSION));
downloadProfile.downloadId = cursor.getInt(cursor.getColumnIndex(DownloadEntry.COLUMN_NAME_DL_ID));
downloadProfiles.add(downloadProfile);
} while (cursor.moveToNext());
}
cursor.close();
return downloadProfiles;
}
答案 0 :(得分:1)
SQL中的字符串文字进入'single quotes'
。不带引号的字符串被解释为标识符,例如列名。
但是,最好使用?
变量并绑定值:
Cursor cursor = db.rawQuery("SELECT * FROM " + DownloadEntry.TABLE_NAME
+ " WHERE " + DownloadEntry.COLUMN_NAME_DL_STATE + " = ?", new String[] { state });