简体   繁体   English

SQLite查询无法正常工作

[英]Sqlite Query is not working properly

I have created a database with a String column which contains dates in String format (dd/mm/yyyy). 我创建了一个包含String列的数据库,其中包含String格式(dd / mm / yyyy)的日期。 I want to fetch the data from that table which is between two given dates, but when I tried with the below query, I found that it doesn't make any difference what month and year I have selected; 我想从该表中获取两个给定日期之间的数据,但是当我尝试以下查询时,我发现选择的月份和年份没有任何区别; it compares the "dd" field only from "dd/mm/yy". 它只比较“ dd / mm / yy”中的“ dd”字段。

The below query will display all the data which is between day 14 to 25 from every month and year. 下面的查询将显示每月和每年第14到25天之间的所有数据。 I want data between the given date, month, and year. 我需要给定日期,月份和年份之间的数据。

Select * from RunningLog 
where CAST(RunDate AS DATETIME) between CAST('14/04/2011' AS DATETIME) and 
      CAST('25/04/2011' AS DATETIME)

Please see my answer here about how dates are (or are not) stored in sqlite3. 在此处查看我的答案,以了解如何在sqlite3中存储(或不存储)日期。 Sqlite doesn't have a date field, so its actually stored as a string. Sqlite没有日期字段,因此它实际上存储为字符串。 Trying to sort / filter on this will prove to be difficult. 尝试对此进行排序/过滤将很困难。 Instead use an int field, and store the time in milliseconds. 而是使用int字段,并以毫秒为单位存储时间。

I prefer INTEGER / Unix time storage, then use the built in date and time functions to format when pulling from DB. 我更喜欢INTEGER / Unix时间存储,然后在从数据库中提取数据时使用内置的日期和时间函数进行格式化。

Example: 例:

long millis = Calendar.getTimeInMillis();

Store millis in the database as an integer. 将Millis作为整数存储在数据库中。 Then, refer to the first link on how to use the date and time functions in sqlite3. 然后,参考第一个链接,了解如何在sqlite3中使用日期和时间函数。

Sqlite3 documentation does not say it can cast a datetime: http://www.sqlite.org/lang_expr.html (refer to Cast expressions numeral). Sqlite3文档没有说它可以转换日期时间: http ://www.sqlite.org/lang_expr.html(请参阅Cast表达式数字)。 Why don't you do that from Java? 您为什么不使用Java做到这一点? I mean, how are you storing the dates in the database? 我的意思是,您如何将日期存储在数据库中? I highly recommend saving a long (Unix time); 我强烈建议您节省很长的时间(Unix时间); that way you can easily get a couple of long numbers that represent an exact date and time and use them to query your table. 这样,您就可以轻松地获得几个代表确切日期和时间的长数字,并使用它们来查询表。

Recomendation: Use Datetime fields in the database and JodaTime as a Time library. 建议:使用数据库中的Datetime字段并将JodaTime用作时间库。

Is quite easy to parse the datetime into a DateTime object and then you have many useful methods to compare and work with dates. 将日期时间解析为DateTime对象非常容易,然后您可以使用许多有用的方法来比较和处理日期。

Also, your SQL queries will be better. 而且,您的SQL查询会更好。

You can compute the number of seconds between two dates. 您可以计算两个日期之间的秒数。 Here is an example: SELECT strftime('%s','now') - strftime('%s','2004-01-01 02:34:56'); 这是一个示例:SELECT strftime('%s','now')-strftime('%s','2004-01-01 02:34:56');

Based on the sign of the difference you can say if one date is before another. 根据差异的符号,您可以说一个日期是否早于另一个日期。 In your case you have to do two comparisons, so you have to verify the sign of two differences. 在您的情况下,您必须进行两个比较,因此您必须验证两个差异的符号。 Here you can find other examples, maybe they give you other ideas (http://www.sqlite.org/cvstrac/wiki?p=DateAndTimeFunctions). 在这里您可以找到其他示例,也许它们可以为您提供其他建议(http://www.sqlite.org/cvstrac/wiki?p=DateAndTimeFunctions)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM