Sqlite行没有删除

时间:2012-02-25 07:09:12

标签: ios objective-c iphone sqlite delete-row

当我尝试从我的iphone应用程序中删除一行时,我可以正确获取该ID,但该行未删除。

-(void)delete_profile:(NSString *)mID {

    NSLog(@"menuID: %@",mID);
    sqlite3 *database;
    sqlite3_stmt *deleteStmt=nil;
    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
    if(deleteStmt == nil) {
        const char *sql = "delete from item where menuid = ?";
        if(sqlite3_prepare_v2(database, sql, -1, &deleteStmt, NULL) != SQLITE_OK)
            NSAssert1(0, @"Error while creating delete statement. '%s'", sqlite3_errmsg(database));
    }

    //When binding parameters, index starts from 1 and not zero.
    sqlite3_bind_int(deleteStmt, 1, [mID integerValue]);

    if (SQLITE_DONE != sqlite3_step(deleteStmt)) 
        NSAssert1(0, @"Error while deleting. '%s'", sqlite3_errmsg(database));

    sqlite3_reset(deleteStmt);    
    }
    sqlite3_close(database);

}

2 个答案:

答案 0 :(得分:7)

请检查您的menuid数据类型。并在准备或使用之前进行绑定,例如以下示例

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

   NSString *sql = [NSString stringWithFormat:@"delete from item where menuid =%d",[mID intValue]];

                const char *del_stmt = [sql UTF8String];

                sqlite3_prepare_v2(contactDB, del_stmt, -1, & deleteStmt, NULL);
                if (sqlite3_step(deleteStmt) == SQLITE_DONE)
                {

                } else {

                }
                sqlite3_finalize(deleteStmt);
                sqlite3_close(contactDB);


}

答案 1 :(得分:0)

必须检查您的dbPath和db名称(区分大小写)

-(void)delete_profile:(NSString *)mID {
{
    NSString *sql_str=[NSString stringWithFormat:@"DELETE FROM item where menuid = %d",[mID intValue]];
    const char *sql = [sql_str UTF8String];

    sqlite3 *database;

    if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
    {
        sqlite3_stmt *deleteStmt;
        if(sqlite3_prepare_v2(database, sql, -1, &deleteStmt, NULL) == SQLITE_OK)
        {

            if(sqlite3_step(deleteStmt) != SQLITE_DONE )
            {
                NSLog( @"Error: %s", sqlite3_errmsg(database) );
            }
           else
            {
              //  NSLog( @"row id = %d", (sqlite3_last_insert_rowid(database)+1));
                  NSLog(@"No Error");
            }
        }
        sqlite3_finalize(deleteStmt);
    }
    sqlite3_close(database);
}