Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FMDB ios no such table

Tags:

sqlite

ios

fmdb

I have a problem with a sqlite project that i'm doing, I'm using FMDB, I follow a simple example, but doesn´t work. And I can't find the error. I did my database schema from the terminal, I put some data on it. I'm very new to ios dev, so I don't know exactly if I did the steps ok. This is what I did:

1 - I made my Database schema and add some fields. 2 - I copied the database.db into my project folder in xcode. 3 - I add the FMDB files. 4 - I add the sqlite3.dylib 5 - I put this code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.dbname = @"database.db";
    NSArray * docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * docDIr = [docPath objectAtIndex:0];
    self.dbpath = [docDIr stringByAppendingPathComponent:dbname];

    [self checkDB];

    [self getQ];

    return YES;
}
-(void) getQ
{
    FMDatabase * db = [FMDatabase databaseWithPath:dbpath];
    [db open];

    FMResultSet * result = [db executeQuery:@"SELECT * FROM table1"];

    NSLog(@"last Error: %@",[db lastErrorMessage]);
    NSLog(@"result: %@", result);
}
-(void) checkDB
{
    BOOL success;
    NSFileManager * fm = [NSFileManager defaultManager];
    success = [fm fileExistsAtPath:dbpath];
    NSError * error = [[NSError alloc] init];
    if (success) return;
    NSLog(@"result:");
    NSString * dbPathFromApp = [[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:self.dbname];

    [fm copyItemAtPath:dbPathFromApp toPath:dbpath error:&error];           
}

Apparently the database it's empty, so what happened? why I can't find table1 ? If I open the file with any sqlite gui, the table appears just fine. Thank's for any help The console show me the next lines: 2013-03-02 14:03:31.839 myApp[21433:c07] last Error: no such table: table1 2013-03-02 14:03:31.841 myApp[21433:c07] result: (null)

like image 396
Fede Avatar asked Mar 02 '13 17:03

Fede


1 Answers

A couple of thoughts:

  1. I'd confirm the database has been added to your bundle. Go to your target settings, select "Build Phases", expand "Copy Bundle Resources", and make sure your database is listed there. If not, click on the "+" button and add it.

    bundle

  2. I'd reset your simulator. If you ever ran the app and didn't copy the database properly, it would have created a blank database for you in your documents folder. By resetting your simulator, you can get rid of any blank databases that might be there.

  3. If it's still not working, I'd check the bundle database in your simulator. Navigate to the simulator folder (~/Library/Application Support/iPhone Simulator), find the app, open up the bundle (by control clicking on the app itself and say "show package contents"), and check the database in there and make sure it has your table1.

like image 63
Rob Avatar answered Oct 15 '22 16:10

Rob