I am preparing sqlite statement and while preparing this statement my code is breaking.I am using following line of code
if (sqlite3_prepare_v2(database,getCC , -1, &getConsumptionCount, NULL) != SQLITE_OK)
{
NSAssert1(0, @"Error: failed to prepare for getConsumptionCount statement with message '%s'.", sqlite3_errmsg(database));
}
which works perfectly fine on simulator but breaks on iPhone.
My database db.sql was is resource folder which I shifted to other resources folder. What is the reason why I get SIGABRT error? Like I know it is becoz of NSAssert1 which I put there but why my application is failing to prepare for the statement.
When I see in the debugger its saying no such table, But I am confused since My database is there and have the table. Is there anything I am doing wrong. How to resolve this.
I am doing it the following way
databaseName = @"CaloriePacerDatabase.sql";
// Setup some globals
// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
inFormat = [[NSDateFormatter alloc] init];
[inFormat setDateFormat:@"yyyy-MM-dd"];
databasePath = [[NSString alloc] initWithString: [documentsDir stringByAppendingPathComponent:databaseName]];
[self checkAndCreateDatabase];
[self openDatabase];
[self compileStatements];
return self;
}
- (void) checkAndCreateDatabase
{
// Check if the SQL database has already been saved to the users phone, if not then copy it over
BOOL success;
// Create a FileManager object, we will use this to check the status
// of the database and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the database has already been created in the users filesystem
success = [fileManager fileExistsAtPath:databasePath];
// If the database already exists then return without doing anything
if(success) return;
// If not then proceed to copy the database from the application to the users filesystem
// Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
[fileManager release];
}
- (void) openDatabase
{
if(sqlite3_open([databasePath UTF8String], &database) != SQLITE_OK)
{
NSAssert1(0, @"Error: failed to open Database '%s'.", sqlite3_errmsg(database));
}
}
- (void) compileStatements
{
//int i = sqlite3_prepare_v2(database,getCC , -1, &getConsumptionCount, NULL);
NSLog(databasePath);
if ( sqlite3_prepare_v2(database,getCC , -1, &getConsumptionCount, NULL)!= SQLITE_OK) {
NSAssert1(0, @"Error: failed to prepare for getConsumptionCount statement with message '%s'.", sqlite3_errmsg(database));
}
Please let me know if it is not the right way to do it.