从表sqlite中删除行的问题

时间:2013-04-08 05:17:19

标签: ios sqlite tableview

这就是我在标题中所做的事情

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

@implementation SQLAppDelegate

@synthesize window;
@synthesize navigationController;
@synthesize coffeeArray;

这就是我用来删除原始

的内容
    - (void) removeCoffee:(NSNumber *)coffeeObj {

      NSLog(@"coffeeObj%@",coffeeObj);

        int myInteger = [coffeeObj integerValue];
        NSLog(@"myInteger%d",myInteger);
      // print this myInteger0


        NSLog(@"%@",coffeeArray);
       //print object
   if (sqlite3_open([self getDBPath], &database) == SQLITE_OK)
   {    
   NSLog(@"myInteger%@",[self getDBPath]);

        NSString *sql = [NSString stringWithFormat: @"delete from Coffee where CoffeeID =%d",myInteger];

                             const char *del_stmt = [sql UTF8String];
       NSLog(@"%@",del_stmt); // getting print 
 // print this  delete from Coffee where CoffeeID =0.

                             sqlite3_prepare_v2(database, del_stmt, -1, & deleteStmt, NULL);
                 NSLog(@"sqlite3_step(deleteStmt) ==           SQLITE_DONE%@",sqlite3_step(deleteStmt) == SQLITE_DONE);

    // this print null

                             if (sqlite3_step(deleteStmt) == SQLITE_DONE)
                             { 
                               //NSLog(@"hi") this is not getting print

                             } else {
                                  //NSLog(@"hi") this is getting print
                             }
                             sqlite3_finalize(deleteStmt);
                             sqlite3_close(database);
    [coffeeArray removeObjectAtIndex:myInteger];
        NSLog(@"%@",coffeeArray);
       // object is deleted 
    }
}

我的表格如下

table name = Coffee

CoffeeID(INTEGER)=0

CoffeeName(VARCHAR)=Latte

Price(REAL)=2.99

事物运行完美的对象从数组中删除,这就是为什么它不出现在表格单元格上。但它没有从数据库表中删除,这就是为什么当我再次启动应用程序然后它再次显示请帮助我做错了。

2 个答案:

答案 0 :(得分:2)

在开始删除对象之前,只要正确打开数据库,就一致。试试吧。

//Setting path 
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

      NSString *docsDir = [dirPaths objectAtIndex:0];

    databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"database.db"]];

    const char *dbpath=[databasePath UTF8String];

        if (sqlite3_open(dbpath, &database) == SQLITE_OK)
        {
      NSString *sql = [NSString stringWithFormat: @"delete from Coffee where CoffeeID =%d",myInteger];

                             const char *del_stmt = [sql UTF8String];

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

                             } else {

                             }
                             sqlite3_finalize(deleteStmt);
                             sqlite3_close(database);
    [coffeeArray removeObjectAtIndex:myInteger];
        NSLog(@"%@",coffeeArray);
       // object is deleted 

        }

答案 1 :(得分:0)

if(sqlite3_open([[self filepath] UTF8String], &db) == SQLITE_OK)
    {
        Deletestatement = nil;  

        if(Deletestatement == nil)
        {
            const char *sql = "delete from Sqlitemanager2;";

            if(sqlite3_prepare_v2(db, sql, -1, &Deletestatement, NULL) != SQLITE_OK)
                NSAssert1(0, @"Error while creating delete statement. '%s'", sqlite3_errmsg(db));
        }

        if (SQLITE_DONE != sqlite3_step(Deletestatement)) //prathibha for problem in if
            NSAssert1(0, @"Error while deleting. '%s'", sqlite3_errmsg(db));

        sqlite3_finalize(Deletestatement);

    }
I hope this will help you.