FMDB ios no such table
Asked Answered
P

2

10

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)

Picardi answered 2/3, 2013 at 17:12 Comment(0)
G
24

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.

Goerke answered 2/3, 2013 at 17:20 Comment(6)
Thank you very much! that's it, steps 1 and 2 works perfectly! I forget a step, newbie mistake :DPicardi
Your point 2 is what I forgot. I didn't copy the database from the bundle to the documents directory.Importance
Thank you very much!~ Step #1 was what I needed to do :)Cigar
There doesnt seem to be iPhone Simulator folder in my Application Support folder. Even though im running my application on a simulatorVessel
No, it's moved since 2013. It's now in "~/Library/Developer/Core Simulator/Devices". Unfortunately, these folders are now using cryptic GUID numbers (but you can often find it by sorting by date). Anyway, once you find the appropriate device, then navigate to "data/Containers/Data/Application" to find the various sandboxes for your apps.Goerke
Just passing by, to avoid searching for my kids, I put this line in my code : >> print ("Database Created : \r\ropen ( NSString(string: db.databasePath()!).deletingLastPathComponent ) \r\r") << Then I can copy from the debugger console and paste in the terminal to open in the finder..Overskirt
B
-1

After adding the Database file please select that and Tick Mark Target Membership.

Check the attached file

Burrton answered 8/3, 2021 at 10:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.