为什么我的Java代码中的SQLite请求只输出十月份(&&/10/&&)?

huangapple go评论60阅读模式
英文:

Why my SQLite request in java code output only months from October(&&/10/&&)?

问题

案例在我的SQLite数据库中日期存储为DATE类型我试图显示本月的记录如果月份是1011或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.

&#39;&#39;&#39;

        LocalDate ld = LocalDate.now();
        int yearNumber = ld.getYear();
        int monthNumber = ld.getMonth().getValue();

        String queryString = &quot;SELECT &quot; +
                &quot; *, &quot; +
                &quot;strftime(&#39;%m&#39;, DAY_DATE) as Month, &quot; +   //  getting number of month from 01 to 12 
                &quot;strftime(&#39;%Y&#39;, DAY_DATE) as Year   &quot; +   // getting number of year
                &quot; FROM &quot; + DAYS_TABLE +
                &quot; WHERE &quot;+&quot;Month&quot; +
                &quot; LIKE &quot;  + monthNumber +
                &quot; AND &quot; + &quot;Year&quot;+
                &quot; LIKE &quot; + yearNumber +
                &quot; ORDER BY &quot; + COLUMN_DAY_DATE +
                &quot; DESC &quot; ;
&#39;&#39;&#39;


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(&#39;%m&#39;, DAY_DATE)` may return `05` and not `5`.&lt;br/&gt;
If you want to compare it with a number you must convert it and this can be done implicitly by adding `0`. &lt;br/&gt;
So change to:

     &quot; WHERE (Month + 0) = &quot;  + monthNumber +
     &quot; AND (Year + 0) = &quot; + yearNumber +
     &quot; ORDER BY &quot; + COLUMN_DAY_DATE +
     &quot; DESC &quot; ;



</details>



huangapple
  • 本文由 发表于 2020年9月15日 21:24:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63902842.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定