SQLiteDatabase db = DatabaseProvider.dbHelper.getReadableDatabase();Cursor cursor = db.rawQuery("Select * from " + ConsumerEnquiryTable.TABLE_NAME + " where " + ConsumerEnquiryTable.Cols.USER_LOGIN_ID + "='" + userId + "' AND ( CAST(" + ConsumerEnquiryTable.Cols.DUE_DATE + " as datetime) >= CAST(" + beginDate + " as datetime) AND CAST(" + ConsumerEnquiryTable.Cols.DUE_DATE + " as datetime) <= CAST("+ endDate + " as datetime)) AND " + ConsumerEnquiryTable.Cols.CARD_STATUS + "='" + jobCardStatus + "'", null);我尝试过使用 CAST、strftime、datetime。due_date 列是 varchar 类型,我想选择在 beginDate 和 endDate 之间具有到期日期的记录。所有日期均为 dd/MM/yyyy 格式
2 回答
狐的传说
TA贡献1804条经验 获得超3个赞
如果您将列的DUE_DATE格式更改YYYY-MM-DD为 SQLite 唯一可比较的格式,那么您只需要一个BETWEEN子句并将所有参数作为字符串数组传递,这是避免任何 sql 注入风险的推荐最安全的方法:
Cursor cursor = db.rawQuery(
"SELECT * FROM " + ConsumerEnquiryTable.TABLE_NAME + " WHERE " +
ConsumerEnquiryTable.Cols.USER_LOGIN_ID + " = ? AND (" +
ConsumerEnquiryTable.Cols.DUE_DATE + " BETWEEN ? AND ?) AND " +
ConsumerEnquiryTable.Cols.CARD_STATUS + " = ?",
new String[] {userId, beginDate, endDate, jobCardStatus}
);
当然,参数 beginDate 和 endDate 也必须采用YYYY-MM-DD格式。
慕莱坞森
TA贡献1810条经验 获得超4个赞
您可以使用以下查询
mDb.query(MY_TABLE, null, DATE_COL + " BETWEEN ? AND ?", new String[] { minDate + " 00:00:00", maxDate + " 23:59:59" }, null, null, null, null);
添加回答
举报
0/150
提交
取消