如何在sqlite中插入NSDate?

发布于 2024-08-27 08:44:07 字数 832 浏览 9 评论 0原文

你好,我正在尝试在我的数据库表中插入当前日期 我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

緦唸λ蓇 2024-09-03 08:44:07

为什么不使用:

"Insert into AnwaltHistory (id,dateVisited) values (%d,datetime())",Id

Why not use:

"Insert into AnwaltHistory (id,dateVisited) values (%d,datetime())",Id
烟雨扶苏 2024-09-03 08:44:07

你可以试试这个:

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

You could try this:

double dateVisited = [[NSDate date] timeIntervalSince1970]; //now
// save dateVisited to your 'dateVisited' field
無處可尋 2024-09-03 08:44:07

根据官方文档,SQLITE上的DateTime应该是ISO8601格式(又名:字符串of ("YYYY-MM-DD HH:MM:SS.SSS"))

因此,要存储日期,您应该格式化日期字符串,下面是一个可以帮助您完成此操作的代码:

NSDate *SourceDate = [NSDate date];  // Or whatever NSDATE you like...
NSDateFormatter *MyDateFormatter = [[NSDateFormatter alloc] init];
[MyDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
NSString *SQLiteQuery = [NSString stringWithFormat:@"Insert into YourTable(YourDateField) values ('%@')", [MyDateFormatter stringFromDate:SourceDate]];

这应该可以做到,请大声喊叫如果您需要更多帮助,

干杯
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:

NSDate *SourceDate = [NSDate date];  // Or whatever NSDATE you like...
NSDateFormatter *MyDateFormatter = [[NSDateFormatter alloc] init];
[MyDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
NSString *SQLiteQuery = [NSString stringWithFormat:@"Insert into YourTable(YourDateField) values ('%@')", [MyDateFormatter stringFromDate:SourceDate]];

This should do it, give's a shout if you need more help,

Cheers
H

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文