Cannot load sqlite database in xcode

Sasha Grievus Source

I've created a simple database in sqlite using SqlManager, then i copied the file (Person.sqlite) in my resources in a simple xcode project and used this code to get to the database but the database seems not to load. The function sqlite3_open in the init returns 0 (all ok), while the sqlite3_prepare in getAllPersons returns that cannot find in the database the table "Persona" which is in my database, so i believe the problem it's i cannot get to the real db. Probably the open, not finding the database, create a new empty one... So why this happens? My way of giving the path of the db is not correct? ([[NSBundle mainBundle] pathForResource:@"Persone" ofType:@"sqlite"];) Truly, an nslog on the variable defining the path (sqLiteDb) returns null, but i tried several other ways to call this path but all were wrong...

- (id)init
    self = [super init];
    if (self) {
        NSString *sqlLiteDb = [[NSBundle mainBundle] pathForResource:@"Persone" ofType:@"sqlite"];
        NSLog(@"Path db: %@",sqlLiteDb);
        if(sqlite3_open([sqlLiteDb UTF8String], &database) != SQLITE_OK){
            NSLog(@"Failed to open database");
        }else {
            NSLog(@"Database opened");

    return self;

- (NSArray *) getAllPersons{
    NSMutableArray *returnArray=[[NSMutableArray alloc] init];
    NSString *query= @"SELECT Nome FROM Persona";    
    sqlite3_stmt *statement;
    NSLog(@"%d", sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, NULL));
    printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database) );

    if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, NULL) == SQLITE_OK){
       while (sqlite3_step(statement)==SQLITE_ROW) {
            int uniqueId = sqlite3_column_int(statement, 0);
            char *nomeChar =(char *) sqlite3_column_text(statement, 1);
            char *cognomeChar =(char *) sqlite3_column_text(statement, 2);
            NSString *nome= [[NSString alloc] initWithUTF8String:nomeChar];
            NSString *cognome= [[NSString alloc] initWithUTF8String:cognomeChar];

            PersonInfo *info= [[PersonInfo alloc] initWithUniqueId:uniqueId nome:nome cognome:cognome];
            [returnArray addObject:info];
    return returnArray;


Sorry i believe the solution it's easy, but i'm still not so fond in xcode ^^ Thank you!

UPDATE: No, it has always been "Persone". After two hour of failures and the opening of this question, i've finally thought "So, why don't use a clean? It won't ever work, but give it a chance..." Obviously, it worked... -_-' For some reason the project needed only a clean... Thank you for your help, anyway!

As i said in the update for some reason the project needed only a clean...

Sure call the prepare two time isn't a good idea. Sorry, the code i posted was dirty of code used only for the sake of testing. Good to know that the open could have problem with permissions, i'll check this! Thank you very much for your help!



answered 6 years ago remort #1

sqlite often fails with open becaise of permissions to a DB file and a whole directori file is placed in.

comments powered by Disqus