I have a project that includes FMDB to manage SQLite databases. I imported and linked the FMDB wrappers, but problems is that no results are shown when I query the database :
The sqlite database created with Firefox SQLite manager (Ubuntu) and I copy it to Xcode .
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *path = [docsPath stringByAppendingPathComponent:@"db.sqlite"];
FMDatabase *db = [FMDatabase databaseWithPath:path];
[db open];
FMResultSet *fResult= [db executeQuery:@"SELECT * FROM mytable"];
while([fResult next])
{
NSLog(@"%@",[fResult stringForColumn:@"title"]);
}
[db close];
If you have copied your database into xCode then you should search for your database in your application's main bundle resource path first, then copy it into the Documents directory if it doesn't exist there yet, only then you can work with it. You might want to debug your FMDatabase object using lastErrorMessage
and lastErrorCode
messages.
FMDatabase *db = [FMDatabase databaseWithPath:path];
NSLog(@"database instantiated: %@", db];
[db open];
NSLog(@"Database has encountered an error with message: %@. And code: %d", db.lastErrorMessage, db.lastErrorCode];
FMResultSet *fResult= [db executeQuery:@"SELECT * FROM mytable"];
while([fResult next])
{
NSLog(@"%@",[fResult stringForColumn:@"title"]);
}
[db close];
The other problem might of course sound silly, but if your 'mytable' does not contain anything, the while loop expression will always be false. But my best guess - the database is not in the Documents directory.