在XCODE中更新SQLite时出错

时间:2014-12-11 12:10:42

标签: xcode sqlite

我正在尝试在sqlite表中更新一行,但是当我执行exec命令时出现错误。 我在DBManager类中有一个尝试更新表的方法。

-(void) UpdateContactsTable :(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
                         andField5:(NSString *) field5
                       field5Value:(NSString *) field5Value
                         andField6:(NSString *) field6
                       field6Value:(NSNumber *) field6Value

{

[self openDB];
sqlite3_stmt *updateStmt;
NSString *sql = [NSString stringWithFormat:@"UPDATE CONTACTS SET NAME = ?, SURNAME1 = ?, SURNAME2 = ?, PHONE = ?, EMAIL = ?, PID = ? WHERE PID = '%i'", [field6Value integerValue]];
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);
        sqlite3_bind_text(updateStmt, 5, [field5Value UTF8String], -1, SQLITE_TRANSIENT);
}

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));
}
sqlite3_finalize(updateStmt);
sqlite3_close(db);
}

行:

sqlite3_bind_text(updateStmt, 1, [field1Value UTF8String], -1, SQLITE_TRANSIENT);

返回值0,所以我猜它们是正确的。

我收到的错误信息位于:

sqlite3_exec(db, [sql UTF8String], NULL, NULL, &errmsg);

我得到的返回值为20.而且:

if(SQLITE_DONE != sqlite3_step(updateStmt)){
NSLog(@"Error while updating. %s", sqlite3_errmsg(db));

返回值21.并且表格不会更新。 任何帮助将不胜感激。 非常感谢。

0 个答案:

没有答案