Zach
Zach

Reputation: 10129

sqlite3 update not working in ios

I am trying to update sqlite db. This is the code I am using

if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
    {

        const char * sql;
        sql = "update tms  set name = ?,place=?,stars=?  where id=?";

        sqlite3_stmt *selectStatement;

        //prepare the select statement
        int returnValue = sqlite3_prepare_v2(database, sql, -1, &selectStatement, NULL);
        if(returnValue == SQLITE_OK)
        {
            sqlite3_bind_text(selectStatement, 1,[[payloadDict  valueForKey:@"userName"] UTF8String] , [[payloadDict  valueForKey:@"userName"] length],SQLITE_STATIC);
            sqlite3_bind_text(selectStatement, 2,[[payloadDict  valueForKey:@"locName"] UTF8String], [[payloadDict  valueForKey:@"locName"] length],SQLITE_STATIC);
            sqlite3_bind_int(selectStatement, 3, [[payloadDict  valueForKey:@"starCount"] integerValue]);
            sqlite3_bind_int(selectStatement, 4, [[payloadDict  valueForKey:@"rowid"] integerValue]);
            int success = sqlite3_step(selectStatement);
            if(success == SQLITE_DONE)
            {
                isExist = TRUE;
            }
            else {
                //NSAssert1(0,@"Error: Failed to Update %s",sqlite3_errmsg(database));
            }
    }

I am getting value 101 as success when sqlite3_step is executed. But database is not updated with new values. How can I do this properly? Thanks

Upvotes: 1

Views: 967

Answers (1)

Rob
Rob

Reputation: 437422

I agree with @ott's excellent suggestion of making sure the database is located in the Documents directory (though I would have thought that that would have given you an error).

I'd also double check the value returned by [[payloadDict valueForKey:@"rowid"] integerValue] to make sure it matches a value in the id column for one of the existing rows in your table. If it doesn't match anything, sqlite3_step will return SQLITE_DONE even if nothing was updated.

Also note that you might also want to make sure that the id values are stored as numeric values, not text strings as sqlite is pretty lax about letting you store values in whatever data type you originally specified when you first inserted the data, regardless of how the table was defined), and I'm not sure if a WHERE clause looking for a numeric match will succeed if the data was originally stored as a text value. If you used an id column definition like id INTEGER PRIMARY KEY AUTOINCREMENT, where the system defined the values automatically for you, this isn't an issue, but if you manually populated the id column, it might be something to double check. (Generally it does a pretty good job in interpreting strings as numbers on the fly, but there are some weird situations that are problematic: For example, if you stored a string value of "5,127" in a numeric field, if you later then try to retrieve its numeric value, sqlite won't know what to do with the comma in the text value "5,127" and will interpret the numeric value as 5, not as 5127.)

Upvotes: 1

Related Questions