I am using the firefox sqlite manager to help me building an iphone app. In which I include an UPDATE query, which works perfectly in the iPhone simulator. However, it fails when I run from the real machine (the iphone). There is no error, it just does not update the db.
I am thinking of two possible causes:
1) The db it updates is not the one as in simulator
2) The db I read is not the one that is updated
Does anyone have similar experience?
Code as follows:
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"Exercises.sqlite"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
{
NSLog(@"An error has occured.");
}
NSString *ranID = [@"UPDATE Status SET money = " stringByAppendingFormat:@"%d", money + 100];
const char *sql2 = [ranID UTF8String];
sqlite3_stmt *sqlStatement2;
if(sqlite3_prepare(db, sql2, -1, &sqlStatement2, NULL) != SQLITE_OK)
{
NSLog(@"Problem with prepare statement 2");
}
if (sqlite3_step(sqlStatement2)==SQLITE_ROW)
NSLog(@"succeed");
Thanks in advance.
What happening is that you are trying to write inside the database that is in your bundle, you dont have write access to files in your bundle, you will need to copy it to Documents directory if you want to update the database
To copy the database file
Then always use the new file path (
documentsPath)to access the database