I am new to ios Development. Can you help me to delete a record from a table. Database and query seems fine. But I dont know why its not deleteing the records. I Can see the nslog "profile deleted" in my console. Thanks in advance.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
self.str_databasePath = [documentsDirectory stringByAppendingPathComponent:@"MY_database.sqlite"];
sqlite3 *database = nil;
if (sqlite3_open([self.str_databasePath UTF8String], &database) == SQLITE_OK)
{
NSLog(@"database opened : %@", profileType);
const char* sqlStatement;
if([profilerType isEqualToString:@"personal"]){
sqlStatement = "DELETE FROM TABLE1 where profile_type = 'OWN_PERSONAL'";
}else if([profilerType isEqualToString:@"corporate"]){
sqlStatement = "DELETE FROM TABLE2 where profile_type = 'BANK_PROFILE'";
}
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &statement, NULL) == SQLITE_OK)
{
NSLog(@"profile deleted ");
return YES;
}else{
NSAssert1(0,@"Error: Failed to prepare statement with message %s '.", sqlite3_errmsg(database));
}
// Finalize and close database.
sqlite3_finalize(statement);
sqlite3_close(database);
}
return NO;
It's not deleting records because you're only preparing the query, never executing it.
To execute a prepared query, you must call sqlite3_step
Alternatively, you can use sqlite3_exec which is a convenience function that performs prepare, step and finalize.
As HeikoG said, it's much easier to use Core Data. Core Data is basically an SQLite db, but it's much easier to use. Have a look at this tutorial: http://www.raywenderlich.com/934/core-data-tutorial-getting-started
You will probably save time if you use that instead of using SQL directly, even if you have to re-write your current code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With