如何在sqlite中插入NSDate?
你好,我正在尝试在我的数据库表中插入当前日期 我有一个 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 中并手动运行它,它运行得很好并且有实际日期...... 任何人都可以指导我哪里错了:S....... 请
HELLO i am trying to insert Current date in my database table
i have a column dateVisited of type datetime
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 !");
}
the statement executes successfully but when i actually go and check the table it says invalid date... i dont know why
as i have printed sqlstr i copy paste from the console and paste it in sqlite and run it manually it runs perfectly fine and has the actual date....
can any one guide me where i am mistaken :S.......
please
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不使用:
Why not use:
你可以试试这个:
You could try this:
根据官方文档,SQLITE上的DateTime应该是ISO8601格式(又名:字符串of ("YYYY-MM-DD HH:MM:SS.SSS"))
因此,要存储日期,您应该格式化日期字符串,下面是一个可以帮助您完成此操作的代码:
这应该可以做到,请大声喊叫如果您需要更多帮助,
干杯
H
According to the official documents, the DateTime on SQLITE should be in ISO8601 format (AKA: a string of ("YYYY-MM-DD HH:MM:SS.SSS"))
Therefore, to store the date, you should format your date string, below is a code to help you with this:
This should do it, give's a shout if you need more help,
Cheers
H