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:
- 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
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.
精彩评论