FMDB sqlite的sql查询不起作用

时间:2013-12-23 06:37:55

标签: ios iphone sql sqlite fmdb

我正在使用以下SQL查询创建临时表。但是当我以编程方式执行时,它没有显示任何结果(结果集为空),并且当我在数据库上手动执行它时显示记录。

NSString *query=[NSString stringWithFormat:@"create temp table search2 as select  Observationsid from Observations where admin_id=%d AND teacher_id=%d AND observation_type=%d AND isvalid='yes' AND date BETWEEN '12-20-2013' AND '12-23-2013'",appDelegate.admin_id,appDelegate.teacher_id,appDelegate.observationType];
FMResultSet *results=[appDelegate.database executeQuery:query];

Nslogged查询: 打印结果说明 - > _query:

create temp table search2 as select Observationsid from Observations where admin_id=2 AND teacher_id=1 AND observation_type=2 AND isvalid='yes' AND date BETWEEN '12-20-2013' AND '12-23-2013'

并且数据库中有记录。

1 个答案:

答案 0 :(得分:0)

此SQL语句不应返回结果,因此您应使用executeUpdate方法而不是executeQuery方法。你不应该看到任何结果。如果要查看结果,请执行单独的executeQuery语句,返回search2的结果。 (我假设您出于某种原因正在构建search2表。)

NSString *query=[NSString stringWithFormat:@"create temp table search2 as select  Observationsid from Observations where admin_id=%d AND teacher_id=%d AND observation_type=%d AND isvalid='yes' AND date BETWEEN '12-20-2013' AND '12-23-2013'",appDelegate.admin_id,appDelegate.teacher_id,appDelegate.observationType];
if (![appDelegate.database executeUpdate:query])
    NSLog(@"create error: %@", [appDelegate.database lastErrorMessage]);

query = @"SELECT * FROM search2";
FMResultSet *results = [appDelegate.database executeQuery:query];
if (!results)
    NSLog(@"create error: %@", [appDelegate.database lastErrorMessage]);

顺便说一下,虽然你可以为这个特定的SQL豁免stringWithFormat,但这不是一个好主意。您通常希望使用?占位符,例如

NSString *query = @"create temp table search2 as select  Observationsid from Observations where admin_id=? AND teacher_id=? AND observation_type=? AND isvalid='yes' AND date BETWEEN '12-20-2013' AND '12-23-2013'";
if (![appDelegate.database executeUpdate:query, @(appDelegate.admin_id), @(appDelegate.teacher_id), @(appDelegate.observationType)])
    NSLog(@"create error: %@", [appDelegate.database lastErrorMessage]);

不相关,但我注意到您在日期中使用MM-DD-YYYY格式的文本字符串。那不行。 SQLite本身并不“理解”日期。值得注意的是,如果您以MM-DD-YYYY格式存储日期,则12-21-2052的文本字符串为BETWEEN '12-20-2013' AND '12-23-2013'(因为文本字符串按字母顺序进行比较),这可能与您的意图不同。请参阅Date And Time Functions

但是,幸运的是,FMDB为您进行NSDate转换,将其存储为数值。因此,例如,假设您的表定义如下:

CREATE TABLE IF NOT EXISTS Observations
    (Observationsid   INTEGER PRIMARY KEY AUTOINCREMENT, 
     admin_id         INTEGER, 
     teacher_id       INTEGER,
     observation_type INTEGER,
     isvalid          TEXT,
     date             REAL);

您确实希望使用YYYY-MM-DD,或者更好地依靠FMDatabase为您转换NSDate值。请记住,SQLite没有正确的日期格式,因此请选择适当的格式(例如YYYY-MM-DD格式)或让FMDatabase使用NSDate对象为您处理此问题。

如果您想方便将日期字符串转换为日期,可以使用NSDateFormatter,例如:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"MM-dd-yyyy";

NSDate *someDate = [formatter dateFromString:@"12-22-2013"];
if (![appDelegate.database executeUpdate:@"INSERT INTO Observations (admin_id, teacher_id, observation_type, isvalid, date) VALUES (?, ?, ?, ?, ?)", @(appDelegate.admin_id), @(appDelegate.teacher_id), @(appDelegate.observationType), @"yes", someDate])
    NSLog(@"insert error: %@", [appDelegate.database lastErrorMessage]);

或者,如果我想选择这两个日期之间的日期:

NSDate *startDate = [formatter dateFromString:@"12-20-2013"];
NSDate *endDate   = [formatter dateFromString:@"12-23-2013"];

NSString *query = @"create temp table search2 as select  Observationsid, date from Observations where admin_id=? AND teacher_id=? AND observation_type=? AND isvalid='yes' AND date BETWEEN ? AND ?";
if (![appDelegate.database executeUpdate:query, @(appDelegate.admin_id), @(appDelegate.teacher_id), @(appDelegate.observationType), startDate, endDate])
    NSLog(@"create error: %@", [appDelegate.database lastErrorMessage]);

query = @"SELECT * FROM search2";
FMResultSet *results = [appDelegate.database executeQuery:query];
if (!results)
    NSLog(@"select error: %@", [appDelegate.database lastErrorMessage]);
while ([results next])
    NSLog(@"%d %@", [results intForColumnIndex:0], [results dateForColumnIndex:1]);

如果您想避免使用此NSDate逻辑,您可以有意识地以YYYY-MM-DD格式以文本格式存储日期。这也有效。再次,请参阅Date and Time Functions页面,了解日期的有效文本格式列表。