how to get and auto enter current date in sqlite db in java?
I want to automatically enter the date time of the data into the database table. I am using the Date
field and used timeEntry=DATE('NOW')
but when I did a query, the开发者_C百科 output is "DATE('NOW')
instead of the time.
Can anyone kindly help me out ? Thanks.
First of all - SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:
- 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.
SQLite supports five date and time functions as follows:
- date(timestring, modifier, modifier, ...)
- time(timestring, modifier, modifier, ...)
- datetime(timestring, modifier, modifier, ...)
- julianday(timestring, modifier, modifier, ...)
- strftime(format, timestring, modifier, modifier, ...)
You can replace timestring
with 'now' to get the current datetime value.
For more information please see SQLite date and time functions
If you are inputting the value of timeEntry
via ContentValues
be aware that these values are properly sanitized resulting in timeEntry
being assigned a TEXT value & not the result of the function you are passing.
精彩评论