[英]FMDB sqlite's sql query not working
I am creating a temp table using the following SQL query. 我正在使用以下SQL查询创建一个临时表。 But its not showing any results (result set is empty) when I execute it programmatically, and shows records when I execute it manually on the database. 但是当我以编程方式执行它时,它不显示任何结果(结果集为空),而当我在数据库上手动执行它时,它显示记录。
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: Printing description of results->_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'
and records are there in database. 并且记录在数据库中。
This SQL statement shouldn't return results, so you should use executeUpdate
method rather than a executeQuery
method. 该SQL语句不应返回结果,因此您应该使用executeUpdate
方法而不是executeQuery
方法。 You should not see any results from this. 您不应从中看到任何结果。 If you want to see the results, do a separate executeQuery
statement returning the results from search2
. 如果要查看结果,请执行单独的executeQuery
语句,以从search2
返回结果。 (I assume you're building the search2
table for a reason.) (我认为您出于某种原因正在构建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]);
By the way, while you can get away with stringWithFormat
for this particular SQL, that's not a good idea. 顺便说一句,虽然您可以使用stringWithFormat
来处理此特定的SQL,但这不是一个好主意。 You generally want to use the ?
您通常要使用?
placeholders, eg 占位符,例如
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]);
Unrelated, but I notice that you're using MM-DD-YYYY
format text string for your dates. 无关,但我注意到您在使用MM-DD-YYYY
格式的文本字符串作为日期。 That's not going to work. 那是行不通的。 SQLite doesn't natively "understand" dates. SQLite本身并不“理解”日期。 Notably, if you've stored your dates in MM-DD-YYYY
format, a text string of 12-21-2052
is BETWEEN '12-20-2013' AND '12-23-2013'
(because text strings are compared in alphabetically order), which is probably not what you intended. 值得注意的是,如果你已经存储在您的日期在MM-DD-YYYY
格式的文本字符串12-21-2052
是BETWEEN '12-20-2013' AND '12-23-2013'
(因为文本字符串相比按字母顺序排列),这可能不是您想要的。 See Date And Time Functions . 请参见日期和时间函数 。
But, fortunately, FMDB does the NSDate
conversion for you, storing it as numeric value. 但是,幸运的是,FMDB为您执行了NSDate
转换,并将其存储为数值。 So, for example, let's assume your table was defined as follows: 因此,例如,假设您的表定义如下:
CREATE TABLE IF NOT EXISTS Observations
(Observationsid INTEGER PRIMARY KEY AUTOINCREMENT,
admin_id INTEGER,
teacher_id INTEGER,
observation_type INTEGER,
isvalid TEXT,
date REAL);
You really want to use YYYY-MM-DD
or, better, rely upon FMDatabase
to convert your NSDate
values for you. 您确实想使用YYYY-MM-DD
或者更好地依靠FMDatabase
为您转换NSDate
值。 Remember, SQLite does not have proper date formats, so choose an appropriate format (eg YYYY-MM-DD
format) or let FMDatabase
handle this for you, using NSDate
objects. 请记住,SQLite没有正确的日期格式,因此请选择适当的格式(例如YYYY-MM-DD
格式),或让FMDatabase
使用NSDate
对象为您处理此格式。
If you want to facilitate the conversion of date strings to dates, you can use a NSDateFormatter
, eg: 如果您希望将日期字符串转换为日期,可以使用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]);
Or, if I wanted to select the dates between these two dates: 或者,如果我想选择这两个日期之间的日期:
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]);
If you want to avoid this NSDate
logic, you could alternatively consciously store dates in a text format in YYYY-MM-DD
format. 如果要避免这种NSDate
逻辑,则可以选择有意识地以YYYY-MM-DD
格式的文本格式存储日期。 That works, too. 那也行。 Again, see that Date and Time Functions page for a list of valid text formats for dates. 再次,请参见“ 日期和时间功能”页面,以获取日期的有效文本格式的列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.