iphone NSdate的sqlite datetime数据类型? [英] sqlite datetime data type with iphone NSdate?

查看:31
本文介绍了iphone NSdate的sqlite datetime数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sqlite 有没有办法在创建记录时自动在表中保存时间戳,然后我可以在我的应用程序中抓取并保存为 NSDate?

Is there a way in sqlite to automatically save a timestamp in the table whenever a record has been created that I can then grab and save as an NSDate in my application?

或者我应该在我的 iphone 应用程序中使用当前时间创建 NSdate 对象,然后将其插入到数据库中.如果是这样的话?数据类型应该是什么列?日期时间?

Or should I instead create the NSdate object with the current time in my iphone app, and then insert it into the database. If so? What column should the datatype be for the time? a DATETIME?

推荐答案

我所做的是使用 sqlite 的 current_timestamp(看起来像这样:2009-06-16 12:11:24).为此,只需将 qslite 表的行类型设置为

What I do is use sqlite's current_timestamp (which looks something like this: 2009-06-16 12:11:24). To do this just set the row type of your qslite table to

Data type: "DATETIME"
Allow null: NO
Default value: CURRENT_TIMESTAMP

然后使用这样的 SQL 查询:

Then use an SQL query like this:

@"INSERT INTO 'scores' ('one', 'two', 'three') VALUES ('%d', '%d', '%d')"

忽略日期,使其自动获取当前时间值.

ignoring the date, so that it will automatically get the current time value.

然后要将其转换为 NSDate,您可以像这样使用 NSDateFormatter:

Then to convert this to an NSDate you can use an NSDateFormatter like this:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; //this is the sqlite's format
NSDate *date = [formatter dateFromString:score.datetime];

现在您将该日期作为 NSDate 对象.只是不要忘记释放我们分配的 NSDateFormatter :)

and now you have that date as an NSDate object. Just don't forget to release the NSDateFormatter we allocated :)

这篇关于iphone NSdate的sqlite datetime数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆