I'm having some trouble working with dates on my Android application that uses SQLite. I have a couple questions:
- What type should I use to store dates in SQLite (text, integer, ...)?
- Given the best way to store dates how do I store It properly using ContentValues?
- What's the best way to retrieve the date from the SQLite database?
- How to make a sql select on SQLite, ordering the results by date?
I prefer this. This is not the best way, but a fast solution.
Usually (same as I do in mysql/postgres) I stores dates in int(mysql/post) or text(sqlite) to store them in the timestamp format.
Then I will convert them into Date objects and perform actions based on user TimeZone
SQLite can use text, real, or integer data types to store dates. Even more, whenever you perform a query, the results are shown using format
%Y-%m-%d %H:%M:%S
.Now, if you insert/update date/time values using SQLite date/time functions, you can actually store milliseconds as well. If that's the case, the results are shown using format
%Y-%m-%d %H:%M:%f
. For example:Now, doing some queries to verify if we are actually able to compare times:
You can check the same
SELECT
usingcol2
andcol3
and you will get the same results. As you can see, the second row (126 milliseconds) is not returned.Note that
BETWEEN
is inclusive, therefore...... will return the same set.
Try playing around with different date/time ranges and everything will behave as expected.
What about without
strftime
function?What about without
strftime
function and no milliseconds?What about
ORDER BY
?Works just fine.
Finally, when dealing with actual operations within a program (without using the sqlite executable...)
BTW: I'm using JDBC (not sure about other languages)... the sqlite-jdbc driver v3.7.2 from xerial - maybe newer revisions change the behavior explained below... If you are developing in Android, you don't need a jdbc-driver. All SQL operations can be submitted using the
SQLiteOpenHelper
.JDBC has different methods to get actual date/time values from a database:
java.sql.Date
,java.sql.Time
, andjava.sql.Timestamp
.The related methods in
java.sql.ResultSet
are (obviously)getDate(..)
,getTime(..)
, andgetTimestamp()
respectively.For example:
Since SQLite doesn't have an actual DATE/TIME/TIMESTAMP data type all these 3 methods return values as if the objects were initialized with 0:
So, the question is: how can we actually select, insert, or update Date/Time/Timestamp objects? There's no easy answer. You can try different combinations, but they will force you to embed SQLite functions in all the SQL statements. It's far easier to define an utility class to transform text to Date objects inside your Java program. But always remember that SQLite transforms any date value to UTC+0000.
In summary, despite the general rule to always use the correct data type, or, even integers denoting Unix time (milliseconds since epoch), I find much easier using the default SQLite format (
'%Y-%m-%d %H:%M:%f'
or in Java'yyyy-MM-dd HH:mm:ss.SSS'
) rather to complicate all your SQL statements with SQLite functions. The former approach is much easier to maintain.TODO: I will check the results when using getDate/getTime/getTimestamp inside Android (API15 or better)... maybe the internal driver is different from sqlite-jdbc...