Iphone 如何在sqlite中插入NSDate?

Iphone 如何在sqlite中插入NSDate?,iphone,sqlite,nsdate,insertion,Iphone,Sqlite,Nsdate,Insertion,您好,我正在尝试在数据库表中插入当前日期 我有一个datetime类型的列dateVisited NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"]; //this is the sqlite's format NSDate *stringTime = [NSDate date]; NSString *formattedDateS

您好,我正在尝试在数据库表中插入当前日期 我有一个datetime类型的列dateVisited

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"]; //this is the sqlite's format
NSDate *stringTime = [NSDate date];
NSString *formattedDateStringTime = [formatter stringFromDate:stringTime];

sqlStr = [NSString stringWithFormat:@"Insert into AnwaltHistory (id,dateVisited) values (%d,'%@')",Id formattedDateStringTime];

NSLog(@"%@",sqlStr);
BOOL Res = [appDelegate InsUpdateDelData: sqlStr];
if (Res)
{
   NSLog(@"The Following instrunction is excuted successfully !");
}
else
{
   NSLog(@"The Following instrunction Failed to execute !");
}
语句执行成功,但当我实际去检查表时,它显示无效日期。。。我不知道为什么 因为我已经打印了sqlstr,所以我从控制台复制粘贴并粘贴到sqlite中,然后手动运行它,它运行得非常好,并且有实际的日期。。。。 有人能告诉我哪里错了吗。。。。。。。 请

为什么不使用:

"Insert into AnwaltHistory (id,dateVisited) values (%d,datetime())",Id
你可以试试这个:

double dateVisited = [[NSDate date] timeIntervalSince1970]; //now
// save dateVisited to your 'dateVisited' field

您可以运行sqlite>.schema AnwaltHistory并粘贴输出吗?当我自己手动在db接口上运行查询时,插入AnwaltHistory id、dateVisited值87403、datetime或插入AnwaltHistory id,dateVisited values 1232010/04/07 01:49:55插入正确上述两个查询是my sqlStr的结果,但无法自动运行,而manual.sqlite没有日期时间类型。AnwaltHistory表的模式是什么?非常感谢您的回复。我尝试了sqlStr=[NSString stringWithFormat:@Insert into AnwaltHistory id,DATETVISTED值%d,datetime,id];在调试时,我发现它说sqlStr无效。我使用NSLog查看sqlStr中的内容。插入到AnwaltHistory id中,dateVisited值为87403,datetime很好,语句执行成功,但当我浏览数据库时,它显示id,但时间再次无效,然后我手动运行sqlStr,它完全插入,没有错误。我弄错了:S,很抱歉回复晚了