如何根据android spinners从sqlite db中选择的项目显示两个日期之间的数据?

发布于 2024-12-04 07:09:17 字数 346 浏览 0 评论 0原文

我有旋转器,我们可以从中选择一个项目。还有两个按钮可以选择“开始日期”和“结束日期”。显示数据 基于从类别中选择的项目以及从日期到结束日期之间的数据。如何实现这一目标。

以下查询是否正确 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 correct
SELECT * 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝礼 2024-12-11 07:09:17

好的,您的 YYYY-MM-DD 日期格式看起来不错;确保 TO 和 FROM 日期以相同的方式格式化为字符串。查询:

SELECT * FROM TableName where category = ? and dates >= ? and dates <= ?;

或按照 Pratik 建议

SELECT * FROM TableName where category = ? and dates between ? and ?;

根据要求,这里有一个可能的 SQLiteDatabase 查询语句:

Cursor cursor = db.query("TableName", null,
                         "category = ? and dates between ? and ?", 
                          new String[] {"personal", "2011-09-07", "2011-09-10"},
                          null, null, null);

当然,您可以将 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:

SELECT * FROM TableName where category = ? and dates >= ? and dates <= ?;

or as Pratik suggested

SELECT * FROM TableName where category = ? and dates between ? and ?;

As requested, here's a possible SQLiteDatabase query statement:

Cursor cursor = db.query("TableName", null,
                         "category = ? and dates between ? and ?", 
                          new String[] {"personal", "2011-09-07", "2011-09-10"},
                          null, null, null);

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"

另类 2024-12-11 07:09:17

首先,您应该阅读 SQLite 所理解的 SQL:日期和时间函数,以便您了解SQLite 如何处理查询中的日期和时间。幸运的是,查看您的数据表明您正在使用正确的时间格式

因此,考虑到这一点,您的示例查询将变成

SELECT * FROM TableName WHERE category = ?
 AND FromDate >= date(?) AND ToDate <= date(?)

但是查看您的架构以及您对您可能希望执行的某些答案的响应,例如:

  myDB.query(myTable,null,"category = ? AND dates BETWEEN date(?) AND date(?)",
       new String[]{selectedCategory, startDate, endDate}, null);

不要忘记修剪 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

SELECT * FROM TableName WHERE category = ?
 AND FromDate >= date(?) AND ToDate <= date(?)

however looking at your schema and your response to some of the answers you are probably looking to do something like:

  myDB.query(myTable,null,"category = ? AND dates BETWEEN date(?) AND date(?)",
       new String[]{selectedCategory, startDate, endDate}, null);

DOn't forget to trim startDate and endDate of any spaces.

烂人 2024-12-11 07:09:17

试试这个

SELECT * FROM TableName WHERE category = ? AND datecolum between FromDate AND ToDate

try this

SELECT * FROM TableName WHERE category = ? AND datecolum between FromDate AND ToDate
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文