英文:
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, " + // 从01到12获取月份
"strftime('%Y', DAY_DATE) as Year " + // 获取年份
" FROM " + DAYS_TABLE +
" WHERE "+"Month" +
" LIKE " + monthNumber +
" AND " + "Year"+
" LIKE " + yearNumber +
" ORDER BY " + COLUMN_DAY_DATE +
" DESC " ;
我应该使用什么来查看每个月的记录?
<details>
<summary>英文:</summary>
Case: In my SQLite database date store as DATE type. Iam trying to show records from current month and if that month is 10, 11 or 12 everything is working but month from 1 to 9 is giving empty output.
'''
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 " ;
'''
What should i use to see records from each month?
</details>
# 答案1
**得分**: 0
`strftime()` 函数返回一个字符串而不是一个数字,因此 `strftime('%m', DAY_DATE)` 可能会返回 `05` 而不是 `5`。<br/>
如果您想要将其与数字进行比较,您必须将其转换,可以通过添加 `0` 来隐式完成。<br/>
所以更改为:
" WHERE (Month + 0) = " + monthNumber +
" AND (Year + 0) = " + yearNumber +
" ORDER BY " + COLUMN_DAY_DATE +
" DESC " ;
<details>
<summary>英文:</summary>
The function `strftime()` returns a string and not a number, so `strftime('%m', DAY_DATE)` may return `05` and not `5`.<br/>
If you want to compare it with a number you must convert it and this can be done implicitly by adding `0`. <br/>
So change to:
" WHERE (Month + 0) = " + monthNumber +
" AND (Year + 0) = " + yearNumber +
" ORDER BY " + COLUMN_DAY_DATE +
" DESC " ;
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论