开发者

Saving Timestamp into sqlite

开发者 https://www.devze.com 2023-02-01 01:59 出处:网络
18.11.2009 10:32:00 I want the value in between the above tag(created) to be inserted into the sqlite db for which i have taken a column of type timestamp....

18.11.2009 10:32:00

I want the value in between the above tag(created) to be inserted into the sqlite db for which i have taken a column of type timestamp.... how can i store this value in that column?? please advise..开发者_C百科.


Well, it depends in which format you want to save it in your database.

If you want to save it in the string form, then save it directly by making an object. But, use the datatype of string type.

Another option is to save it using the date datatype, seeing as sqlite doesn't have a dedicated date/time datatype.

Use a formatter to set the date format:

 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd-MM-YYYY HH:MM:SS"];

Then make Date object, and save it.


You should replace dots by hyphens and place months, days and year parts in correct order. According to sqlite docs these are the date and time accepted formats:

  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. DDDDDDDDDD


Two options here (if you want sorting, which I assume you do):

  • Add as a string, but re-order the fields from highest to lowest. After that, it's a simple matter to sort via text. This is the slower option.

2009.11.18 10:32:00

  • Convert to a UNIX timestamp, I.E. seconds since Jan 1st, 1970. This is the fastest option.

1261153920

It is then simple to pull it out using date and time functions.

Note that SQLite does not have a date/time data type.


Make object for date

then use this

[date timeIntervalSinceDate:objDate];

this give time interval beetween the date and objDate. two dates(date and objDate) for finding timeInterval.

Save this by convert it into string.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号