View Single Post
  #4 (permalink)  
Old November 10th, 2013, 02:53 PM
thepianoguy thepianoguy is offline
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

Your issue is most likely tied into how you added the Catalog.db file to your project.
If you dragged and dropped the file, the dialogue will present a couple of options:
Destination - with a "copy" check box - check this
Folders - radio buttons - Create Groups or Create references (groups is default)

Add to Targets - add a checkmark to Catalog

If you used File>Add Files the Add to Targets checkmark to Catalog is checked for you (or since I checked it in the past, maybe it is always checked for me…)

The file must be added to the target.

If you run the program that you sent me with the following addition in the -initializeDatabase method

Code:
    NSString *path = [[NSBundle mainBundle]
                      pathForResource:@"catalog"
                      ofType:@"db"];
    
    // Open the database.
    NSLog(@"path is %@",path);////Added line
you will see that the path is NULL. For whatever reason sqlite3_open() doesn't return an error if the database does not exist. So since you made it to the "Opening database" comment you assumed that you actually had loaded the database. This was not true.

To fix this select your catalog.db file in the Project Navigator and hit "delete" selecting remove reference. Select File>Add Files and drill down in your project to select the catalog.db file. Make sure the Add to targets checkbox is selected. Click Add.
Now when you build and run the project will work. The only issue left to fix is in your -getAllProducts method. (DBAccess.old in the project you sent) Your …finalize is in the wrong place in the loop, so it will terminate after just one iteration. Move it down below the next closing brace '}'.

Code:
                       // Add the product to the products array
            [products addObject:product];
            
            // Finalize the statement to release its resources
////            sqlite3_finalize(statement);/////WRONG PLACE
            
        }
        sqlite3_finalize(statement);////RIGHT PLACE
    }
        else {
            NSLog(@"Problem with the database:");
            NSLog(@"%d", sqlResult);
        }
        return products;
    }
Bob
Reply With Quote