如何根据android spinners从sqlite db中选择的项目显示两个日期之间的数据?
我有旋转器,我们可以从中选择一个项目。还有两个按钮可以选择“开始日期”和“结束日期”。显示数据 基于从类别中选择的项目以及从日期到结束日期之间的数据。如何实现这一目标。
以下查询是否正确 SELECT * FROM TableName WHERE 类别 = ?和起始日期 >= ? AND ToDate <= ?
在 android sqlite 中。任何人都可以帮助我吗?
我的数据库架构
ID |日期 |货币 |商人|支付类型 |类别
1 | 2011年9月9日 |美元|更多 |借记|个人
2 | 2011-9-12 |欧元 |本田 |现金 |商业
I have spinner from which we can choose an item. And also having two buttons to choose FROM date and TO date. Display data Based on the item chosen from category and between FROM date and TO date. How to achieve this.
Is this following query correctSELECT * FROM TableName WHERE category = ? AND FromDate >= ? AND ToDate <= ?
in android sqlite. Can anyone help me pls.
My Db schema
id | dates | currency | merchant | payType | category
1 | 2011-9-9 | dollar | more | debit | personal
2 | 2011-9-12 | euro | honda | cash | Business
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,您的 YYYY-MM-DD 日期格式看起来不错;确保 TO 和 FROM 日期以相同的方式格式化为字符串。查询:
或按照 Pratik 建议
根据要求,这里有一个可能的 SQLiteDatabase 查询语句:
当然,您可以将
selectionArgs
字符串替换为微调器中的值以及 FROM 和 TO 日期。我刚刚注意到,显示的表格中的日期在月份和日期字段中没有前导零;您需要这些才能使比较正常工作,因为您实际上是在比较文本字符串。注:
“2011-09-10”< “2011-9-9”
但“2011-09-10”> “2011-09-09”
OK, your YYYY-MM-DD date format looks good; make sure the TO and FROM dates are formatted as strings the same way. The query:
or as Pratik suggested
As requested, here's a possible SQLiteDatabase query statement:
Where, naturally, you'd replace the
selectionArgs
strings with the values from your spinner and FROM and TO dates.I just noticed that your dates in the table shown do not have leading zeros in the month and day fields; you need these for the comparison to work correctly since you are actually comparing text strings. Note:
"2011-09-10" < "2011-9-9"
but"2011-09-10" > "2011-09-09"
首先,您应该阅读 SQLite 所理解的 SQL:日期和时间函数,以便您了解SQLite 如何处理查询中的日期和时间。幸运的是,查看您的数据表明您正在使用正确的时间格式
因此,考虑到这一点,您的示例查询将变成
但是查看您的架构以及您对您可能希望执行的某些答案的响应,例如:
不要忘记修剪 startDate 和 endDate 中的任何空格。
First of all you should read SQL as Understood by SQLite: Date And Time Functions so you are aware of how SQLite handles date and time in queries. Luckily looking at your data suggests you are using the correct time format
So with this in mind your example query would become
however looking at your schema and your response to some of the answers you are probably looking to do something like:
DOn't forget to trim startDate and endDate of any spaces.
试试这个
try this