From the sqlite3 docs it looks like I should be able to use the following syntax to update a row in a database on my iPhone:
NSString *dbFile = [[NSBundle mainBundle] pathForResource:@"database" ofType:@"db"];
sqlite3 *database = NULL;
if (sqlite3_open([dbFile UTF8String], &database) == SQLITE_OK) {
NSString *sql = [NSString stringWithFormat:@"update mytable set myfirstcolumn=%d, mysecondcolumn=%d where id=%d", int1, int2, int3];
sqlite3_exec(database, [sql UTF8String], MyCallback, nil, nil);
}
sqlite3_close(database);
But it doesn't update the database or even call th开发者_高级运维e callback method. Is this just the wrong syntax, as in sqlite3_exec() cannot perform an update for example?
In my example all the column names are correct and the three int values log as the correct values so I'm kind of out of ideas...
Thanks.
Ah, right, figured out the process now which may answer someone else's query. Basically I had the database in the resource bundle with some basic data for my tests which I could read fine. But because the bundle resource is read only I couldn't write data back to the database.
So the solution is to copy the database into the app's Documents folder at application startup programatically but with code to see if the database already exists there (in order not to overwrite it). Because this folder is read-write there should be no issue in updating the database.
sqlite3_exec usually returns an error code (int), and the last parameter can be a pointer to a string that will set an error message. What do those say?
sqlite3_exec(database, [sql UTF8String], MyCallback, nil, &errorMsg);
精彩评论