Search code examples
iosobjective-csqlitefmdb

FMDB SQL query not returning any data


I am making a request to my FMDB that looks like this

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.

What I am trying to do is return all rows between and including the lstartDate and lendDate that are in the table.

UPDATE: Casting long long to NSNumber worked. 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];

Solution

  • Is result == nil? If so, that means that there was an error and you can look at [database lastErrorMessage] to see what the error was.

    One error jumps out at me, though: When you use ? placeholders in your SQL, you should not use quotation marks. 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.


    Note, as the executeQuery documentation says with regard to the variadic parameters supplied for the ? placeholders:

    Optional parameters to bind to ? placeholders in the SQL statement. These should be Objective-C objects (e.g. NSString, NSNumber, etc.), not fundamental C data types (e.g. int, char *, etc.).

    So in the above example, I've replaced lstartDate and lendDate with @(lstartDate) and @(lendDate). This makes them NSNumber objects.