Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add date in sqlite database

Tags:

I have sqlite database

    private static final String DB_PROCESS_CREATE = "create table "         + DB_TABLE_PROCESS + "(" + PROCESS_ID         + " integer primary key autoincrement, "         + PROCESS_DATE + " date);"; 

I create it db.execSQL(DB_PROCESS_CREATE);

How to add date value in this database? I tried :

String date = new SimpleDateFormat("yyyy.MM.dd").format(Calendar .getInstance().getTime()); ContentValues cv = new ContentValues(); cv.put(db.PROCESS_DATE,Date.valueOf(date)); db.mDB.insert(db.DB_TABLE_PROCESS, null, cv)); 

But then I get error :

"The method put(String, String) in the type ContentValues is not applicable for the arguments (String, Date)". 
like image 201
Kostya Khuta Avatar asked May 24 '13 16:05

Kostya Khuta


People also ask

How do I create a date in SQLite?

First, create a new table named datetime_real . Second, insert the “current” date and time value into the datetime_real table. We used the julianday() function to convert the current date and time to the Julian Day. Third, query data from the datetime_real table.

Is there date in SQLite?

Date and Time Datatype. 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.

What is the date format in SQLite?

The date() function returns the date as text in this format: YYYY-MM-DD. The time() function returns the time as text in this format: HH:MM:SS. The datetime() function returns the date and time as text in their same formats: YYYY-MM-DD HH:MM:SS.


1 Answers

Now when you want to insert date to database, you can use this code.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String date = sdf.format(new Date()); 

In database insert the string 'date'

The date format in sqlite should be of following format:

YYYY-MM-DD YYYY-MM-DD HH:MM YYYY-MM-DD HH:MM:SS YYYY-MM-DD HH:MM:SS.SSS YYYY-MM-DDTHH:MM YYYY-MM-DDTHH:MM:SS YYYY-MM-DDTHH:MM:SS.SSS HH:MM HH:MM:SS HH:MM:SS.SSS now DDDDDDDDDD  

For more details, have a look: http://www.sqlite.org/lang_datefunc.html

like image 83
Homam Avatar answered Oct 31 '22 21:10

Homam