0

这就是我在标题中所做的

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

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

4

2 回答 2

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 

        }
于 2013-04-08T05:24:29.947 回答
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.
于 2013-04-08T07:05:53.483 回答