0

I am having issues with the following code which loads an SQLite database.

- (NSArray *)getDatabase {

NSLog(@"Get Database Called");

    NSMutableArray *retval = [[[NSMutableArray alloc] init] autorelease];
    NSString *query = @"SELECT Description, UniqueID, HexCoords, NoHexCoords, NoAnnots, AnnotText, DescriptionFormatting, HexCoordsPortrait FROM MainTable";
    sqlite3_stmt *statement;
    if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil) 
        == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {

            char *nameChars = (char *) sqlite3_column_text(statement, 0);
            char *desChars = (char *) sqlite3_column_text(statement, 1);
            int uniqueID = sqlite3_column_int(statement, 2);

From using breakpoints I can see that the problem is with the if statement and that the code never gets past this if statement. Can anyone spot what might be wrong ? The code was working a few months ago and I have recently upgraded to xCode 4.3 so might this be the problem ?

Thank in advance.

GuybrushThreepwood
  • 5,598
  • 9
  • 55
  • 113
  • The only reason I can see to behave like this is if _database isn't correctly set up. Does the call to open the database fail perhaps? Also, the last argument should be `NULL`, not `nil` since the parameter is not a pointer to an object. Both compile to 0 currently, so shouldn't cause problems, but I'm not aware of any guarantees they'll remain identical. – Joachim Isaksson Mar 27 '12 at 10:36

1 Answers1

1

Yeah i agree With Joachim. there is a problem sometimes the DB doesnot really connect. what i do is a couple of things. First i add following Code in my Application App Delegate.

- (void) copyDatabaseIfNeeded {

    //Using NSFileManager we can perform many file system operations.
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSString *dbPath = [self getDBPath];
    BOOL success = [fileManager fileExistsAtPath:dbPath]; 

    if(success) {

        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"MyDB.sqlite"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

        if (!success) 
            NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }   
}

Call This Function in ApplicationDidFinishLaunching.

Now remove the data base that is in ur bundle currently. (MAKE SURE U HAD BACKUP OF IT). And (if Possible Delete All the project From Iphone Simulator Folder) Coz sometimes the previous Database is attached. Clean Your project, Add the Data Base in ur bundle. Compile it..

Let Me know if it worked

the Get Path Function

- (NSString *) getDBPath {
        //Search for standard documents using NSSearchPathForDirectoriesInDomains
        //First Param = Searching the documents directory
        //Second Param = Searching the Users directory and not the System
        //Expand any tildes and identify home directories.
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
        NSString *documentsDir = [paths objectAtIndex:0];
        return [documentsDir stringByAppendingPathComponent:@"MYDB.sqlite"];
}
WaaleedKhan
  • 685
  • 7
  • 18
  • Thanks - but what is the method "getDBPath" ? – GuybrushThreepwood Mar 27 '12 at 11:00
  • sorry Forgot to mentioned that – WaaleedKhan Mar 27 '12 at 11:18
  • One more thing i would like to add.. in the function u posted in ur question u set the return type to (NSArray *) its not the best Practice.. U should use NSMutableArray – WaaleedKhan Mar 27 '12 at 11:29
  • Thanks - this helped me find the problem - under Lion I now seem to need to delete the database from actual build folder, clean the project and then drag it back into the project. Great work Apple - you've made xCode 4.3.1 even more glitchy in this respect than it was previously. – GuybrushThreepwood Mar 27 '12 at 15:03
  • i dont think its the problem of Xcode. I m currently using Xcode 4.0.2 and i had the same issue. and spend like half a day to it. I m glad it helped u – WaaleedKhan Mar 27 '12 at 16:42