Dvir Levy
Dvir Levy

Reputation: 8108

xCode sqlite query

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.

Upvotes: 1

Views: 2442

Answers (1)

Rob
Rob

Reputation: 437422

  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));

Upvotes: 3

Related Questions