jeudi 25 juin 2015

Sqlite update statement in Xcode not working

I am trying to update a table in my sqlite database within an iOS app. I have used this update statement before, and it worked. The below statement is returning a sqlite commit value of 0, BUT all the fields in the row to be updated are set to "< null >". I don't get an error message ...

What is wrong with the statement?

-(void) UpdateSMUser :(NSString *) tableName
       withField1:(NSString *) field1
      field1Value:(NSString *) field1Value
        andField2:(NSString *) field2
      field2Value:(NSString *) field2Value
        andField3:(NSString *) field3
      field3Value:(NSString *) field3Value
        andField4:(NSString *) field4
      field4Value:(NSString *) field4Value
{

[self openDB];

sqlite3_stmt *updateStmt;
NSString *sql = [NSString stringWithFormat:@"UPDATE SMUser SET UserID = ?, Profile = ?, SMReg = ?, Date = ? WHERE UserID = '%@'" ,field1Value];

if (sqlite3_prepare_v2(db, [sql UTF8String] , -1,
                       &updateStmt, NULL) == SQLITE_OK)
{

    sqlite3_bind_text(updateStmt, 1, [field1Value UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(updateStmt, 2, [field2Value UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(updateStmt, 3, [field3Value UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(updateStmt, 4, [field4Value UTF8String], -1, SQLITE_TRANSIENT);
}

//  [self openDB];
char* errmsg;
NSLog(@"sqlite commit %d",sqlite3_exec(db, [sql UTF8String], NULL, NULL, &errmsg));
sqlite3_exec(db, [sql UTF8String], NULL, NULL, &errmsg);

if(SQLITE_DONE != sqlite3_step(updateStmt)){
    NSLog(@"Error while updating. %s", sqlite3_errmsg(db));
}
else{
    //   [self clearClick:nil];
}
sqlite3_finalize(updateStmt);
sqlite3_close(db);
}

Thank you very much in advance ... it is driving me crazy!

Aucun commentaire:

Enregistrer un commentaire