1

This must be a stupid problem but i cant get my query to work. Its as if my database is empty (I double checked, its not). Its a simple SELECT * FROM table query. This is how i try it:

+(MyDatabase *)database{
    if (database == nil) {
        database = [[MyDatabase alloc] init];
    }
    return database;
}
- (id)init
{
    self = [super init];
    if (self) {
        NSString *sqliteDb = [[NSBundle mainBundle] pathForResource:@"personsDB" ofType:@"sqlite3"];
        if (sqlite3_open([sqliteDb UTF8String], &database) != SQLITE_OK) {
            NSLog(@"Fail to open Database.");
        }
    }
    return self;
}

and

-(NSArray *)getAllRows{
    NSMutableArray *retArray =  [[NSMutableArray alloc] init];
    NSString *query = @"SELECT * FROM persons";

    sqlite3_stmt *statement;
    NSLog(@"checking SQLITE_OK?");
    if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK) {
        NSLog(@"SQLITE_OK");
        while (sqlite3_step(statement) == SQLITE_ROW) {
            int personID = sqlite3_column_int(statement, 0);
            char *personChars = (char *) sqlite3_column_text(statement, 1);
            int gender = sqlite3_column_int(statement, 2);
            int related = sqlite3_column_int(statement, 3);

            NSString *person = [[NSString alloc] initWithUTF8String:personChars];
            MyDBInfo *info = [[MyDBInfo alloc] initWithpersonID:personID person:person gender:gender related:related ];

            [retArray addObject:info];
        }
        sqlite3_finalize(statement);
    }
    return retArray;
}

I think this is all the interesting stuff.

In my Log I get checking SQLITE_OK?, but no SQLITE_OK. I'm not getting Fail to open Database. so I'm assuming that its all good there.

My Database is full and there is a table called persons. I'm very new to sqlite in iOS apps.

Thank you.

3
  • yes, in the personsDB. Commented Jun 26, 2012 at 22:23
  • 1
    Are you sure the db is included in your bundle? I sometimes have added files to my xcode project and they're not automatically included. Select the target in the top of the project navigator, click on the target, select the target in the main panel, select "Build Phases", and see if your db is included in the "Copy Bundle Resources". Commented Jun 27, 2012 at 0:08
  • Needless to say, I believe your code is fine (other than sqlite3_open creating the db and sqlite3_prepare_v2 not looking at it's error message). It's got to be the absence of the db, which will be discovered if you sqlite3_open_v2([sqliteDb UTF8String], &database, SQLITE_OPEN_READWRITE, NULL); or if you look at the sqlite3_errmsg(database) when your sqlite3_prepare_v2 fails (it will probably say "table not found"). Commented Jun 27, 2012 at 0:21

1 Answer 1

3
  1. Are you sure the db is included in your bundle? The default behavior of sqlite3_open is SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, so it will be created on your device/simulator if it wasn't there already, so you won't see Fail to open Database. Use sqlite3_open_v2 if you don't want SQLITE_OPEN_CREATE. Anyway, I sometimes have added files to my xcode project and they're not automatically included in my bundle. Select the target in the top of the project navigator, click on the target, select the target in the main panel, select "Build Phases", and see if your db is included in the "Copy Bundle Resources".

  2. Probably unrelated, but I always copy the database from the bundle to documents folder (if it's not there already).

  3. If I don't receive the expected SQLITE_OK, I always look at my return code or log my error message and error codes (and this would probably report that the table in question was not found, which would have let you identify the problem).

Thus,

NSLog(@"%s db err '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(contactDB), sqlite3_errcode(contactDB));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I was going crazy :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.