Saving Timestamp into sqlite

大憨熊 提交于 2019-12-13 03:59:31

问题


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


回答1:


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



回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/4537114/saving-timestamp-into-sqlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!