I have a sqlite database called "ProductDatabase.sql" that I copied to the application project directory:
/Users/jacknutkins/Documents/TabbedDietApp/TabbedDietApp/ProductDatabase.sql
In the application application delegation class, I have this piece of code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //Set-up some globals m_DatabaseName = @"ProductDatabase.sql"; //Get the path to the documents directory and append the databaseName NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; m_DatabasePath = [documentsDirectory stringByAppendingPathComponent:@"ProductDatabase.sql"]; //Execute the "checkAndCreateDatabase" function [self checkAndCreateDatabase]; //Query the databse for all animal records and construct the "animals" array [self readProductsFromDatabase]; ....
In this moment:
m_DatabasePath = '/ Users / jacknutkins / Library / Application Support / iPhone Simulator / 5.0 / Applications / 6D5BBE3A-BC9A-4C44-B089-FABA27CFFF4B / Library / ProductDatabase.sql'
Here is the code for the other 2 methods:
- (void) checkAndCreateDatabase { NSError * error;
If I execute some NSLogs:
l_DatabasePathFromApp = / Users / jacknutkins / Library / Application Support / iPhone Simulator / 5.0 / Applications / 6D5BBE3A-BC9A-4C44-B089-FABA27CFFF4B / TabbedDietApp.app / ProductDatabase.sql
and
error = Domain error = NSCocoaErrorDomain Code = 260 "Operation could not be completed. (Cocoa error 260.)" UserInfo = 0x6b6d060 {NSFilePath = / Users / jacknutkins / Library / Application Support / iPhone Simulator / 5.0 / Applications / 6D5BBE3A-BC9A- 4C44-B089-FABA27CFFF4B / TabbedDietApp.app / ProductDatabase.sql, NSUnderlyingError = 0x6b6cfa0 "The operation could not be completed. There is no such file or directory"}
I am not sure which file it cannot find here.
- (void) readProductsFromDatabase { //Init the products array m_Products = [[NSMutableArray alloc] init]; NSLog(@"%@", m_DatabasePath); //Open the database from the users filessystem if(sqlite3_open([m_DatabasePath UTF8String], &database) == SQLITE_OK) { //Set-up the SQL statement and compile it for faster access const char *sqlStatement = "select * from products"; sqlite3_stmt *compiledStatement; if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { NSLog(@"Success.."); //Loop through the results and add them to the feeds array while(sqlite3_step(compiledStatement) == SQLITE_ROW) { //Read the data from the results row NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; NSString *aCategory = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)]; NSString *aCalories = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)]; NSString *aFat = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)]; NSString *aSaturates = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)]; NSString *aSugar = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 6)]; NSString *aFibre = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 7)]; NSString *aSalt = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 8)]; NSString *aImageURL = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 9)]; NSLog(@"Delegate"); NSString *aNote = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 10)]; NSUInteger myInt = sqlite3_column_int(compiledStatement, 11); NSString *aServes = [NSString stringWithFormat:@"%d", myInt]; //Create a new animal object with the data from the database Product *l_Product = [[Product alloc] initWithName:aName category:aCategory calories:aCalories fat:aFat saturates:aSaturates sugar:aSugar fibre:aFibre salt:aSalt imageURL:aImageURL note:aNote serves:aServes]; //Add the animal object to the animals array [m_Products addObject:l_Product]; } } //Release the compiled statement from memory sqlite3_finalize(compiledStatement); } sqlite3_close(database); }
Database
declared in the .h file as follows:
In the above method, the line:
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
does not evaluate SQLITE_OK because the database that I am trying to copy to the document directory is empty.
I tried cleaning and assembling, deleting an empty copy of the database and restarting it, etc., but it always copies the empty database.
I searched this several times, and I tried everything I could find without success.
Any help would be greatly appreciated.
Jack
EDIT
If I execute 'select * from products' from a terminal window in the database in the project directory, I will return the expected results.