0

Im using FMDB in my iphone app (SDK 6.1). I had 3 DB's working using FMDB in this app. I added another DB file to the same app. This one should also return results, but in fact its just returning 0 results. If I used sqlite3 in the unix prompt and replicate my query (select * from tablename) I see results.

Anyone see anything wrong with this code? I turned on error checking as well and get no errors when I run the code. Ive tried deleting the app and re-installing it both in simulator and on iphone and get same results.

Thanks for any help!

FMDatabase *database = [FMDatabase databaseWithPath:file];
database.logsErrors = YES;
if (![database open])
{
    [Log log:TERR :@"ERROR Opening Database <%@>", file];
    return FILE_NOT_FOUND;
}

NSString *selquery = [[[NSString alloc] initWithFormat:@"SELECT * FROM %@",
                       tablename] autorelease];
[Log log:TINFO :@"selquery: %@", selquery];
FMResultSet *results = [database executeQuery:selquery];

NSUInteger count = [database changes];  // how many rows affected apparently
if (!count)
{
    [Log log:TINFO :@"NO RESULTS FOUND, count = %d", count];
    return NO_SQL_RESULTS;
}

if ([database hadError])
{
    NSString *msg = [[[NSString alloc] initWithFormat:@"Database Error <%@> %d: %@", dbname, [database lastErrorCode], [database lastErrorMessage]] autorelease];
    [Log log:TERR :msg];
    [Log log:TSERVLOG :msg];
    return DB_ERROR;
}

SearchItem *item = nil;
while ([results next])
{
Tim
  • 647
  • 1
  • 10
  • 21
  • The [`changes`](http://ccgus.github.io/fmdb/html/Classes/FMDatabase.html#//api/name/changes) method calls the [`sqlite3_changes`](http://www.sqlite.org/c3ref/changes.html) function, which is relevant to update/insert/delete queries only. You should simply proceed with the returning of values, as fattomhk pointed out. Keep track of number of records returned as you iterate through your while loop, and move your `if (count == 0)` logic to after the `while` loop. – Rob Aug 02 '13 at 06:18

1 Answers1

0

I think NSUInteger count = [database changes]; would not apply to select query.

Try to skip that if block.

You may include FMDatabaseAdditions.h header file to use intForQuery:.

Horst
  • 1,733
  • 15
  • 20
  • Thanks for that suggestion. Just added it, same problem still however. – Tim Aug 02 '13 at 05:52
  • Can it go into while ([results next])? – Horst Aug 02 '13 at 06:00
  • Ok, bizarre. It works now, and not sure what changed. Spent 3 hours on this issue, just glad its ok now and learned something new. Thank you. – Tim Aug 02 '13 at 06:13