简体   繁体   中英

SQLite does not have a DATE datatype? How can I workaround this?

SQLite doesn't have a data type for dates.

I was wondering if it's enough to make string comparisons between date strings like Ymd H:i:s (the standard sql datetime format).

For example ...WHERE date < NOW()... . would this fail in certain situations?

You can store dates in SQLite with the following data types:

  • TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
  • REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 BC according to the proleptic Gregorian calendar.
  • INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.

You can then convert these fake dates using the functions listed here .

Now, you also mentioned function NOW() and that won't work in SQLite. That's for MySQL. This will show you SQLite syntax to use:

sqlite> select date('now');
2012-02-12
sqlite> select date('now') = date('2012-02-12');
1
sqlite> select date('now') = date('2012-02-11');
0

So, it is highly recommended for you to use this functions and, on the other side, make sure you don't use NOW() .

Quoted From Roger

SQLite doesn't have dedicated datetime types , but does have a few datetime functions . Follow the string representation formats (actually only formats 1-10) understood by those functions (storing the value as a string) and then you can use them, plus lexicographical comparison on the strings will match datetime comparison (as long as you don't try to compare dates to times or datetimes to times, which doesn't make a whole lot of sense anyway).

Depending on which language you use, you can even get automatic conversion . (Which doesn't apply to comparisons in SQL statements like the example, but will make your life easier.)

Personally I like using unsigned INT and unix timestamps for dates. It is very easy and efficient to compare dates as integers, and PHP has numerous functions for making these dates human readable.

http://php.net/manual/en/function.date.php

Using now() won't work because SQLite doesn't know what the means. SQLite does, however, know what current_timestamp means and that has the desired format :

The format for CURRENT_TIMESTAMP is "YYYY-MM-DD HH:MM:SS".

In MySQL, current_timestamp is a synonym for now() and that also has the desired format :

Returns the current date and time as a value in 'YYYY-MM-DD HH:MM:SS' .

So they both use ISO-8601 timestamp formats and just about any database will be able to convert between ISO-8601 strings to and timestamps quite easily so that is a good and portable choice.

I'm pretty certain it would fail in certain situations. A faster method would be storing time() values (or mktime()) and generating queries based on time().

This would also work with MySQL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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