This is very similar to another question that got closed as not a real question. I tried to edit it to make it valid for reopening but was told I would be better off asking a new question.
I'm developing on android and need to store datetime values in a sqlite database to track repeating events that will generate notifications. I will also need to be able to query the database based on time ranges.
The SQLite documentation states that it does not support specific date types but that dates can be represented using TEXT, REAL, or INTEGER 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 B.C. according to the proleptic Gregorian calendar.
INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
The advantages of each initially seem to be:
Does this sound correct? Would using an INTEGER for datetimes make querying time ranges noticeably faster than when using TEXT? Anything else I haven't considered?
Given my use case, which of these solutions would be best?
SQLite does not support built-in DateTime storage a class, but SQLite allows us to work with timestamp types.
The datetime() function returns the date and time as text in their same formats: YYYY-MM-DD HH:MM:SS.
Using INTEGER to store SQLite date and time values First, create a table that has one column whose data type is INTEGER to store the date and time values. Second, insert the current date and time value into the datetime_int table. Third, query data from the datetime_int table. It's an integer.
TEXT useful for readability in the database with ability to be displayed directly later (no transformation necessary)
The ISO format is usually not used for display; you have to transform this, too. (This format is more useful for debugging.)
costly if calculations need to be performed on them
With databases, the bottleneck usually is the I/O. I doubt you will ever see a query where the actual format of date values would make a noticeable difference.
Introduces possible error from time zones.
The TEXT format does not have a time zone specifier, but neither have the other formats. If you say that all your DB values are in UTC, there is no difference.
REAL useful for dates before 1970
All formats support all years between 0 and 9999. (Integers can be negative.)
Does not represent time of day, only days.
Time of day is represented as a fractional value.
INTEGER ... very good compatibility since it is a widely supported standard.
But Java uses milliseconds, not seconds.
Anything else I haven't considered?
The default output format of most of the built-in date functions is TEXT.
Given my use case, which of these solutions would be best?
I'd say TEXT, but I don't think there would be much of a difference.
Does not represent time of day, only days.
That's what the fractional portion of the REAL
is for.
Would using an INTEGER for datetimes make querying time ranges noticeably faster than when using TEXT?
Most likely. I cannot envision a scenario in which string comparisons would be faster than integer comparisons, particularly for indexed columns in queries.
Anything else I haven't considered?
I have no idea if you have considered the effects of gamma rays on man-in-the-moon marigolds, but that's not important here. :-)
Given my use case, which of these solutions would be best?
INTEGER
, IMHO.
Answers here are going to be primarily opinion, but if I were you I'd use the INTEGER type and store the unix timestamp. It seems less dependent on format conversions/parsing, and is the most universally supported standard.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With