从数据库中删除行时崩溃

时间:2013-04-06 13:55:34

标签: ios sqlite

我将此代码用于此事

这是标题

#import "SQLAppDelegate.h"
#import "RootViewController.h"
#import "Coffee.h"

static sqlite3 *database = nil;
static sqlite3_stmt *deleteStmt = nil;

@implementation SQLAppDelegate

这是删除方法

- (void) removeCoffee:(NSNumber *)coffeeObj {

  NSLog(@"coffeeObj%@",coffeeObj);
    NSInteger myInteger = [coffeeObj integerValue];
    int i = [coffeeObj integerValue];
    NSLog(@"myInteger%d",myInteger);
    NSLog(@"i%d",i);
  // this print index number of the row which was selected 
    if(deleteStmt == nil) {

        const char *sql = "delete from Coffee where coffeeID = ?";
    //coffeeID is primary key it will be like 0,1,2,3,4...


        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, myInteger);

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

    sqlite3_reset(deleteStmt);



    [coffeeArray removeObjectAtIndex:myInteger];
}

由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'创建删除语句时出错。 '出

请帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

假设您打算说错误是“内存不足”,那么这通常意味着您实际上没有打开数据库。确保您的database变量引用了实际打开的数据库连接。