带有 NSDate 的 FMDB 查询执行但不生效

发布于 2024-11-25 11:04:02 字数 1020 浏览 2 评论 0原文

我通过 FMDB 执行的任何包含 NSDates 的查询都会执行,但不会返回结果集或对数据库进行编辑。我正在 ios 4.3 模拟器中工作。未规定日期的其他查询正确执行。下面是我使用的代码摘录:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh-mm-ss"];

NSDate *start = [dateFormatter dateFromString:@"2011-07-18 06:40:21" ];
NSDate *end = [dateFormatter dateFromString:@"2011-07-18 06:41:19"];

[dateFormatter release];

FMDatabase* db = [FMDatabase databaseWithPath:appDelegate.databasePath];
if (![db open]) {
NSLog(@"Could not open db.");
}

[db beginTransaction];
[db executeUpdate:@"delete from time_stamp_table where time_stamp >= ? 
and time_stamp <= ?",start,end];

NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
[db commit];
[db close];

架构在 time_stamped_table 中声明了 time_stamp:

create table time_stamp_table{
id INTEGER PRIMARY KEY,
time_stamp DATETIME NOT NULL
}

当我在命令行上的 sqlite3 中使用存储在模拟应用程序上下文中的数据库进行这些查询时,它们可以工作。是否有一些我缺少的 NSDate 使用 FMDB 查询的特殊准备?

Any query I execute through FMDB that include NSDates execute but do not return result sets or make edits to the database. I am working in the ios 4.3 simulator.Other Queries that do not stipulate a date execute correctly. Heres an exerpt of the code that i use:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh-mm-ss"];

NSDate *start = [dateFormatter dateFromString:@"2011-07-18 06:40:21" ];
NSDate *end = [dateFormatter dateFromString:@"2011-07-18 06:41:19"];

[dateFormatter release];

FMDatabase* db = [FMDatabase databaseWithPath:appDelegate.databasePath];
if (![db open]) {
NSLog(@"Could not open db.");
}

[db beginTransaction];
[db executeUpdate:@"delete from time_stamp_table where time_stamp >= ? 
and time_stamp <= ?",start,end];

NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
[db commit];
[db close];

The schema states time_stamp in the time_stamped_table thusly:

create table time_stamp_table{
id INTEGER PRIMARY KEY,
time_stamp DATETIME NOT NULL
}

When I make these queries in sqlite3 on the command line, using the database stored on the simulated Application context, they work. Is there some special preperation for querying by NSDate with FMDB that I am missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

软糖 2024-12-02 11:04:02

查询就像

 SELECT *  from Time_Stamp where updatedTime >= DATE('2011-05-17 06:40:21') AND updatedTime <= DATE('2011-07-19 06:40:21')

在比较日期时获取正确的记录。您可能会从中得到一些提示并更改您的查询,例如

 [db executeUpdate:@"delete from time_stamp_table where time_stamp >= DATE(?) 
and time_stamp <= DATE(?)",start,end];

或者

 [db executeQuery:[NSString stringwithFormat:@"delete from time_stamp_table where time_stamp >= DATE('%@') 
and time_stamp <= DATE('%@')",start,end];

我还没有测试查询,所以您必须自己测试它。希望有帮助

Query like

 SELECT *  from Time_Stamp where updatedTime >= DATE('2011-05-17 06:40:21') AND updatedTime <= DATE('2011-07-19 06:40:21')

fetches the correct records while comparing the dates. You might take some hint from this and change your query something like

 [db executeUpdate:@"delete from time_stamp_table where time_stamp >= DATE(?) 
and time_stamp <= DATE(?)",start,end];

or

 [db executeQuery:[NSString stringwithFormat:@"delete from time_stamp_table where time_stamp >= DATE('%@') 
and time_stamp <= DATE('%@')",start,end];

I have not tested the queries so You will have to test it yourself. Hope it helps

耶耶耶 2024-12-02 11:04:02

尽量避免将 NSDate 解析为 SQL 查询。而是尝试以 sqlite 可以理解的格式解析 NSString:'2011-07-18 06:41:19'。
如果您希望 sqlite 比较您必须使用特定日期函数的值。 Sqlite3 为您提供了 DATE、TIME 或 DATETIME 函数来转换字符串。

其次,要纠正 Rahul Sharma,使用 DATE 函数比较 TIMESTAMP 会失败,因为 TIMESTAMP 会同时保存日期和时间。因此应使用 DATETIME 函数。

[db executeUpdate:@"delete from time_stamp_table where time_stamp >= DATETIME(?) 
                                                   and time_stamp <= DATETIME(?);",
                    @"2011-07-18 06:40:21",
                    @"2011-07-18 06:41:19"]; 

Try to avoid parsing a NSDate to a SQL-Query. Rather try parsing a NSString in a format sqlite may understand: '2011-07-18 06:41:19'.
If you would like sqlite to compare the values you have to use the a specific date function. Sqlite3 provides you the DATE, TIME or DATETIME functions to convert your strings.

Secondly, to correct Rahul Sharma, using the DATE function to compare a TIMESTAMP would fail because TIMESTAMP is saving both date and time. Therefore the DATETIME function should be used.

[db executeUpdate:@"delete from time_stamp_table where time_stamp >= DATETIME(?) 
                                                   and time_stamp <= DATETIME(?);",
                    @"2011-07-18 06:40:21",
                    @"2011-07-18 06:41:19"]; 
━╋う一瞬間旳綻放 2024-12-02 11:04:02

可能您使用过 executeQuery 方法。 删除操作属于更新操作。起初,我使用了executeQuery,但一次又一次检查属性类型也找不到问题。最后 executeUpdate 修复了它。

May be you have used executeQuery method. Delete operation belongs to updating operation. At first, I've used executeQuery, but couldn't find the problem even checking the property type once and once again. Finally executeUpdate fixes it.

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