gpt4 book ai didi

ios - 从表sqlite中删除行的问题

转载 作者:行者123 更新时间:2023-11-28 22:36:21 25 4
gpt4 key购买 nike

这就是我在标题中所做的

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

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

最佳答案

在开始删除对象之前,只要确认数据库是否正确打开即可。就这样试试吧。

//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

}

关于ios - 从表sqlite中删除行的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15871775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com