更新sqlite表中的行

时间:2014-01-17 02:51:42

标签: ios sql objective-c sqlite

我是xcode的新手,如果一行已经存在,我正在尝试更新sqlite表。当我运行此代码时,它会构建但不执行updateAlumni查询。它记录“更新!”虽然。

sqlite3_stmt *stmt;
    IF (sqlite3_prepare(alumniDB, update_stmt, -1, &stmt, nil)== SQLITE_OK) {
        IF (sqlite3_step(stmt) == SQLITE_ROW) {
        NSLog(@"update!");
        const CHAR *updateAlumni = [[NSString stringWithFormat:@"UPDATE ALUMNI SET EMAIL = ?, PHONE = ?, ADDRESS = ?, BIRTHDAY = ? WHERE FIRSTNAME = '%s' AND LASTNAME = '%s'", [ContactFirstName UTF8String], [ContactLastName UTF8String]] UTF8String];

        sqlite3_bind_text(stmt, 1, [ContactEmail UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(stmt, 2, [ContactPhone UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(stmt, 3, [ContactAddress UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(stmt, 4, [ContactBday UTF8String], -1, SQLITE_TRANSIENT);

        IF(sqlite3_exec(alumniDB, updateAlumni, NULL, NULL, &error)==SQLITE_OK){
            NSLog(@"Alumni updated");
        }
    }
    ELSE{
        NSLog(@"add!");
        NSString *ADD = [NSString stringWithFormat:@"INSERT OR REPLACE INTO ALUMNI(FIRSTNAME,LASTNAME,EMAIL,PHONE,ADDRESS,BIRTHDAY) VALUES('%s','%s','%s','%s','%s','%s')",[ContactFirstName UTF8String],[ContactLastName UTF8String],[ContactEmail UTF8String],[ContactPhone UTF8String],[ContactAddress UTF8String],[ContactBday UTF8String]];
        const CHAR *addAlumni = [ADD UTF8String];

        IF(sqlite3_exec(alumniDB, addAlumni, NULL, NULL, &error)==SQLITE_OK){
            NSLog(@"Alumni added");
        }
    }
}

0 个答案:

没有答案