Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting current date and time in SQLite database

I want to create a table in SQLite in which one of the field is for date, in which date and time of current instance should save. Which data type should I use?

I'm planning to use 'timestamp'. How to insert current timestamp value to the field? Also how to write content values for this date field?

like image 891
Jesbin MJ Avatar asked Mar 18 '13 09:03

Jesbin MJ


People also ask

How do I get todays date in SQLite?

The SQLite function DATE('now') returns the current date as a text value. It uses the 'YYYY-MM-DD' format, where YYYY is a 4-digit year, MM is a 2-digit month, and DD is a 2-digit day of the month. This function takes one argument: the text 'now' indicates the current date and time.

Does SQLite have datetime?

SQlite does not have a specific datetime type. You can use TEXT , REAL or INTEGER types, whichever suits your needs.

How does SQLite store time in database?

This example demonstrate about How to store values with current time in Android sqlite. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


3 Answers

SQLite supports the standard SQL variables CURRENT_DATE, CURRENT_TIME, and CURRENT_TIMESTAMP:

INSERT INTO Date (LastModifiedTime) VALUES(CURRENT_TIMESTAMP) 

The default data type for dates/times in SQLite is TEXT.

ContentValues do not allow to use generic SQL expressions, only fixed values, so you have to read the current time in Java:

cv.put("LastModifiedTime",        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); 
like image 178
CL. Avatar answered Sep 21 '22 21:09

CL.


INSERT INTO Date (LastModifiedTime) VALUES(DateTime('now')) 

Use this site for further reference.

like image 28
Premsuraj Avatar answered Sep 21 '22 21:09

Premsuraj


To get the current local(system) time, add the 'localtime' option:

select datetime('now', 'localtime');

like image 33
David Garoutte Avatar answered Sep 18 '22 21:09

David Garoutte