简体   繁体   English

FMDB SQL 查询不返回任何数据

[英]FMDB SQL query not returning any data

I am making a request to my FMDB that looks like this我正在向我的 FMDB 发出如下请求

long long lstartDate = 1467331200000;
long long lendDate = 1468108800000;


    [_queue inDatabase:^(FMDatabase *database) {
        FMResultSet * result = [database executeQuery:@"SELECT * FROM dayEPOCTable WHERE date >= '?' AND date <= '?' ", lstartDate, lendDate];

However when the query has finished I do not have any items available in the result.但是,当查询完成时,结果中没有任何可用项目。

The lendDate & lstartDate both have hard coded values that exists in the table, I have gone to Devices, downloaded the container to check to make sure those values appear on the SQL db. lendDate 和 lstartDate 都具有表中存在的硬编码值,我去了 Devices,下载了容器以检查以确保这些值出现在 SQL 数据库中。

What I am trying to do is return all rows between and including the lstartDate and lendDate that are in the table.我想要做的是返回表中的 lstartDate 和lendDate 之间的所有行。

UPDATE : Casting long long to NSNumber worked.更新:将 long long 转换为 NSNumber 有效。 updated code is as follows更新后的代码如下

NSNumber *lstartDate = [NSNumber numberWithLongLong:1467331200000];
     NSNumber *lendDate = [NSNumber numberWithLongLong:1468108800000];

[_queue inDatabase:^(FMDatabase *database) {
            FMResultSet * result = [database executeQuery:@"SELECT * FROM dayEPOCTable WHERE date >= '?' AND date <= '?' ", lstartDate, lendDate];

Is result == nil ? result == nil吗? If so, that means that there was an error and you can look at [database lastErrorMessage] to see what the error was.如果是这样,那意味着有错误,您可以查看[database lastErrorMessage]以查看错误是什么。

One error jumps out at me, though: When you use ?但是,一个错误突然出现在我身上:当您使用? placeholders in your SQL, you should not use quotation marks. SQL 中的占位符,则不应使用引号。 So remove those:所以删除那些:

FMResultSet * result = [database executeQuery:@"SELECT * FROM dayEPOCTable WHERE date >= ? AND date <= ?", @(lstartDate), @(lendDate)];
NSAssert(result, @"executeQuery error: %@", [database lastErrorMessage]);

There certainly can be lots of other possible sources of problems, but this is where I'd start, namely eliminating the quotation marks and checking to see if executeQuery succeeded or not, and if it failed retrieve the SQLite error message.当然还有很多其他可能的问题来源,但这是我要开始的地方,即消除引号并检查executeQuery成功,如果失败,则检索 SQLite 错误消息。


Note, as the executeQuery documentation says with regard to the variadic parameters supplied for the ?请注意,正如executeQuery文档中所说,关于为? placeholders:占位符:

Optional parameters to bind to ?要绑定到的可选参数? placeholders in the SQL statement. SQL 语句中的占位符。 These should be Objective-C objects (eg NSString , NSNumber , etc.), not fundamental C data types (eg int , char * , etc.).这些应该是 Objective-C 对象(例如NSStringNSNumber等),而不是基本的 C 数据类型(例如intchar *等)。

So in the above example, I've replaced lstartDate and lendDate with @(lstartDate) and @(lendDate) .所以在上面的例子中,我用@(lstartDate)@(lendDate)替换了lstartDatelendDate This makes them NSNumber objects.这使它们成为NSNumber对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM