2

Does anyone have an idea how to insert current date or bind the current date in sqlite for iphone app?

I have used this, to store a String:

sqlite3_bind_text( compiledStatement, 3, [Gjourney UTF8String], -1, SQLITE_TRANSIENT);

Is there any way to store the current date? In my datebase I make the Table hello in which i have the column startdate with a DATETIME dataType.

Thanks

Jacob
  • 41,721
  • 6
  • 79
  • 81
Shima
  • 144
  • 13

3 Answers3

2

I think try using like this it may help you.

dateExpires = [NSDate dateWithTimeIntervalSinceNow: sqlite3_column_double(compiledStatement, 3)];

Cheers.....

Sabby
  • 2,586
  • 2
  • 27
  • 41
  • What it will do can it take starting date auotmetically And please explain me more – Shima Jul 13 '11 at 06:50
  • can i set data type as DATETEXT for start date – Shima Jul 13 '11 at 06:54
  • Try to use the sqlite3_column_double instead of binding it as a text.Because the time stamp would be saved as double value.Thats why your app is crashing.....Just check it whether it helps you or not.. – Sabby Jul 13 '11 at 06:55
  • No My app is not getting crash It is successfully insert the value but When I am firing the (select startdate from hello ) it was not giving answer .that's way ask the can change the datatype as datetext – Shima Jul 13 '11 at 06:58
  • What code you compiled,did you use my code or your.And moreover have you set the data type as time stamp in your SQLite while you created the table there – Sabby Jul 13 '11 at 07:23
  • I select your answer because it near to my question – Shima Jul 13 '11 at 08:43
2

SQLite understands date/time values entered in the following formats. If the format is invalid, SQLite will not give an error, but the function call will return null.

  1. YYYY-MM-DD
  2. YYYY-MM-DD HH:MM
  3. YYYY-MM-DD HH:MM:SS
  4. YYYY-MM-DD HH:MM:SS.SSS
  5. YYYY-MM-DDTHH:MM
  6. YYYY-MM-DDTHH:MM:SS
  7. YYYY-MM-DDTHH:MM:SS.SSS
  8. HH:MM
  9. HH:MM:SS
  10. HH:MM:SS.SSS
  11. now
  12. DDDD.DDDD (julian day as floating-point number)

So code as follows,

NSDate *today=[NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
NSString *dateString=[dateFormat stringFromDate:today];
[dateFormat release];

Just insert this dateString value, SQLite will recognize it as a date.

KingofBliss
  • 15,055
  • 6
  • 50
  • 72
0
NSString *date = [NSDate date];

it returns currunt date. save this date string to SQlite;

iOSPawan
  • 2,884
  • 2
  • 25
  • 50