为什么我的 java 代码中的 SQLite 请求仅输出从 10 月开始的几个月(&&/10/&&)?
Why my SQLite request in java code output only months from October(&&/10/&&)?
案例:在我的 SQLite 数据库中,日期存储为 DATE 类型。我正在尝试显示当月的记录,如果那个月是 10、11 或 12,一切正常,但从 1 到 9 的月份给出空输出。
'''
LocalDate ld = LocalDate.now();
int yearNumber = ld.getYear();
int monthNumber = ld.getMonth().getValue();
String queryString = "SELECT " +
" *, " +
"strftime('%m', DAY_DATE) as Month, " + // getting number of month from 01 to 12
"strftime('%Y', DAY_DATE) as Year " + // getting number of year
" FROM " + DAYS_TABLE +
" WHERE "+"Month" +
" LIKE " + monthNumber +
" AND " + "Year"+
" LIKE " + yearNumber +
" ORDER BY " + COLUMN_DAY_DATE +
" DESC " ;
'''
我应该用什么来查看每个月的记录?
函数 strftime()
return 是一个字符串而不是数字,所以 strftime('%m', DAY_DATE)
可能 return 05
而不是 5
。
如果你想将它与一个数字进行比较,你必须转换它,这可以通过添加 0
来隐式完成。
所以改为:
" WHERE (Month + 0) = " + monthNumber +
" AND (Year + 0) = " + yearNumber +
" ORDER BY " + COLUMN_DAY_DATE +
" DESC " ;
案例:在我的 SQLite 数据库中,日期存储为 DATE 类型。我正在尝试显示当月的记录,如果那个月是 10、11 或 12,一切正常,但从 1 到 9 的月份给出空输出。
'''
LocalDate ld = LocalDate.now();
int yearNumber = ld.getYear();
int monthNumber = ld.getMonth().getValue();
String queryString = "SELECT " +
" *, " +
"strftime('%m', DAY_DATE) as Month, " + // getting number of month from 01 to 12
"strftime('%Y', DAY_DATE) as Year " + // getting number of year
" FROM " + DAYS_TABLE +
" WHERE "+"Month" +
" LIKE " + monthNumber +
" AND " + "Year"+
" LIKE " + yearNumber +
" ORDER BY " + COLUMN_DAY_DATE +
" DESC " ;
'''
我应该用什么来查看每个月的记录?
函数 strftime()
return 是一个字符串而不是数字,所以 strftime('%m', DAY_DATE)
可能 return 05
而不是 5
。
如果你想将它与一个数字进行比较,你必须转换它,这可以通过添加 0
来隐式完成。
所以改为:
" WHERE (Month + 0) = " + monthNumber +
" AND (Year + 0) = " + yearNumber +
" ORDER BY " + COLUMN_DAY_DATE +
" DESC " ;