使用SQLite3从数据库中删除联系人。 Delete Method正在跳过if语句并显示else

时间:2013-04-26 16:02:04

标签: ios6 sqlite

我无法删除记录。代码看起来不错,我在Xcode端没有出现任何错误,但是当我运行应用程序并尝试删除记录时,它会跳过if并直接转到else显示我的错误代码:

Error: Failed to delete contact. 

我遵循了所有说明,所有其他功能都运作良好。我必须使用SQLite3进行删除。代码有什么问题?

- (IBAction)deleteData:(id)sender {
    sqlite3_stmt *statement;
    const char *dbpath = [_databasePath UTF8String];

    if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
    {
        NSString *deleteSQL = [NSString stringWithFormat:@"DELETE FROM CONTACTS (name, address, phone)VALUES (\"%@\", \"%@\", \"%@\")",
                               self.name.text, self.address.text, self.phone.text];

        const char *delete_stmt = [deleteSQL UTF8String];
        sqlite3_prepare_v2(_contactDB, delete_stmt, -1, &statement, NULL);
        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            self.status.text = @"Contact Deleted";
            self.name.text = @"";
            self.address.text = @"";
            self.phone.text = @"";
        } else {
            self.status.text = @"Error! Failed to delete contact";
        }
        sqlite3_finalize(statement);
        sqlite3_close(_contactDB);
    }
}

1 个答案:

答案 0 :(得分:0)

您的DELETE语法错误。 请阅读the documentation

您可能希望删除由其ID值标识的记录:

DELETE FROM Contacts WHERE id = ?