I’m using a sqlite3 db that I open at the app delegate and then at another view after pressing a button I execute an update statement that does not work.
The app delegate code:
- (BOOL) openDB{
//NSMutableArray *logoArray = [[NSMutableArray alloc] init];
@try {
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"SportsLogo.sqlite"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
{
return NO;
}
}
@catch (NSException *exception) {
NSLog(@"An exception occured: %@", [exception reason]);
}
@finally {
return YES;
}
}
- (sqlite3 *) getDB{
return db;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
//MySportsLogosDB *myDB = [[MySportsLogosDB alloc]init];
//NSMutableArray *myArray = [[NSMutableArray alloc]init];
if ([self openDB] != YES){
NSLog(@"Problem with opening the DB");
return NO;
}
return YES;
}
Then when I press the button I run the following code:
-(IBAction)enter:(id)sender
{
UIImage *answerImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@%@",currentTitle, @".png"]];
if ([[currentTitle lowercaseString] isEqualToString:logoNameText.text]){
[imageGuessView setImage:answerImage];
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
// NSString *sql =[NSString stringWithFormat:@"select rowid from mytable where name = %@",str];
NSString *sql = [NSString stringWithFormat:@"UPDATE LogosTbl set logoStatus = ?1 WHERE logoName = '%@'",[currentTitle lowercaseString]];
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare_v2([appDelegate getDB], [sql UTF8String], -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"Problem with prepare statement");
NSLog(@"%s",sqlite3_errmsg([appDelegate getDB]));
// printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database) );
}
if(SQLITE_DONE != sqlite3_step(sqlStatement)){
NSLog(@"Problem with UPDATE");
}
}
else{
NSLog(@"First No");
}
}
The prepare is OK and also the step is OK but I don’t see that the data was updated.
The sqlite file is at my project tree in Xcode and it’s placed at the project directory in the file system with read & write permissions.
Thank you
If the database file is in the project’s bundle then you can’t write anything there, as the bundle is readonly. Copy the database file to your documents folder and then you will have a writeable database. This example uses the FMDB wrapper for sqlite3: