Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlite3 update row syntax

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 the 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.

like image 495
Iain Hemstock Avatar asked Aug 04 '26 13:08

Iain Hemstock


1 Answers

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.

like image 65
Iain Hemstock Avatar answered Aug 06 '26 03:08

Iain Hemstock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!