2

I want to insert current date in SQLite database datetime type field. How can I do this?

UPDATE : Using mDb.execSQL("INSERT INTO " + DATABASE_TABLE + " VALUES (datetime()) ");

I am able to insert like date_created=2012-10-10 13:02:08 and I want this format 10 Oct 2012 12:48 how to achive this?

Ankit HTech
  • 1,863
  • 6
  • 31
  • 42

3 Answers3

10

I prefer the following ways to get it done:

  1. Create the table with a default settings to put current datetime automatically. For example:

    ColumnName DATETIME DEFAULT CURRENT_TIMESTAMP
    
  2. Create SimpleDateFormat and convert current date into SQL format string. For example:

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String strDate = sdf.format(new Date());
    
    ContentValues values = new ContentValues();
    values.put("ColumnName", strDate);
    
waqaslam
  • 67,549
  • 16
  • 165
  • 178
1

You can use:

mDb.execSQL("INSERT INTO " + DATABASE_TABLE + " VALUES (datetime()) ");

check: https://stackoverflow.com/a/819605/1434631

Community
  • 1
  • 1
Nermeen
  • 15,883
  • 5
  • 59
  • 72
  • Thanks for the response I used it and it is giving result like date_created=2012-10-10 13:02:08 and I want this format 10 Oct 2012 12:48 how to achive this. – Ankit HTech Oct 10 '12 at 07:37
0
   ContentValues coloumnVlues = new ContentValues();
   //SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
   Date date = new Date();
   coloumnVlues.put("fname", fname);

   // inserted date in database like May 31, 2013 10:42:53 AM it's simulator datetime
   coloumnVlues.put("created_date", DateFormat.getDateTimeInstance(). format(date));

their is no need to use SimpleDateFormat.

you get datetime from database the example given in below link

http://www.sqlite.org/lang_datefunc.html

Kailas
  • 3,173
  • 5
  • 42
  • 52