带有 NSDate 的 FMDB 查询执行但不生效
我通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查询就像
在比较日期时获取正确的记录。您可能会从中得到一些提示并更改您的查询,例如
或者
我还没有测试查询,所以您必须自己测试它。希望有帮助
Query like
fetches the correct records while comparing the dates. You might take some hint from this and change your query something like
or
I have not tested the queries so You will have to test it yourself. Hope it helps
尽量避免将 NSDate 解析为 SQL 查询。而是尝试以 sqlite 可以理解的格式解析 NSString:'2011-07-18 06:41:19'。
如果您希望 sqlite 比较您必须使用特定日期函数的值。 Sqlite3 为您提供了 DATE、TIME 或 DATETIME 函数来转换字符串。
其次,要纠正 Rahul Sharma,使用 DATE 函数比较 TIMESTAMP 会失败,因为 TIMESTAMP 会同时保存日期和时间。因此应使用 DATETIME 函数。
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.
可能您使用过
executeQuery
方法。 删除操作属于更新操作。起初,我使用了executeQuery
,但一次又一次检查属性类型也找不到问题。最后executeUpdate
修复了它。May be you have used
executeQuery
method. Delete operation belongs to updating operation. At first, I've usedexecuteQuery
, but couldn't find the problem even checking the property type once and once again. FinallyexecuteUpdate
fixes it.