gpt4 book ai didi

iphone - iOS Sqlite3 删除查询不删除行

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:54:46 25 4
gpt4 key购买 nike

我正在使用这段代码为我的 ipad 应用程序从数据库中删除一行,

-(BOOL) removeSegmentWithSegmentId:(NSInteger)sId
{
AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];

sqlite3_stmt *statement;

NSString *removeKeyword =[NSString stringWithFormat:@"DELETE FROM segment WHERE segment.segment_id = %d",sId];
const char *query = [removeKeyword UTF8String];
NSLog(@"%@",removeKeyword);

//if(sqlite3_prepare_v2(appDelegate->globalConnection,[removeKeyword UTF8String] , -1, &statement, NULL) == SQLITE_OK)
if(sqlite3_prepare_v2(appDelegate->globalConnection,query , -1, &statement, NULL) == SQLITE_OK)
{
if(sqlite3_step(statement) == SQLITE_DONE) {
sqlite3_finalize(statement);
return YES;
}
}
return NO;
}

但是它不起作用,谁能指导我吗?

最佳答案

您的方法是否返回 YES?

一些事情:

  • 总是记录 sqlite3_errmsg 任何失败
  • 现在,你只是在做 sqlite3_finalizesqlite3_step 返回 SQLITE_DONE,而你真的应该在你成功的时候做 sqlite3_prepare_v2

因此,我建议至少:

-(BOOL) removeSegmentWithSegmentId:(NSInteger)sId
{
BOOL success = NO;

AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];

sqlite3_stmt *statement;

NSString *removeKeyword = [NSString stringWithFormat:@"DELETE FROM segment WHERE segment.segment_id = %d",sId];

if (sqlite3_prepare_v2(appDelegate->globalConnection, [removeKeyword UTF8String], -1, &statement, NULL) == SQLITE_OK)
{
if(sqlite3_step(statement) == SQLITE_DONE)
{
success = YES;
}
else
{
NSLog(@"%s: step not ok: %s", __FUNCTION__, sqlite3_errmsg(appDelegate->globalConnection));
}
sqlite3_finalize(statement);
}
else
{
NSLog(@"%s: prepare failure: %s", __FUNCTION__, sqlite3_errmsg(appDelegate->globalConnection));
}

return success;
}

假设此方法始终返回 YES,如果您没有看到删除的记录,那一定是它没有找到要删除的记录。 (这不被认为是 SQLite 失败。SQL 已成功执行,但无法满足 WHERE 子句。)您可以通过定义以下方法来验证这一点:

- (NSInteger)countSegmentWithSegmentId:(NSInteger)sId
{
NSInteger count = 0;

AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];

sqlite3_stmt *statement;

NSString *sql = [NSString stringWithFormat:@"SELECT segment_id FROM segment WHERE segment.segment_id = %d", sId];

if (sqlite3_prepare_v2(appDelegate->globalConnection, [sql UTF8String], -1, &statement, NULL) == SQLITE_OK)
{
while ((rc = sqlite3_step(statement)) == SQLITE_ROW)
count++;

sqlite3_finalize(statement);
}
else
{
NSLog(@"%s: prepare failure: %s", __FUNCTION__, sqlite3_errmsg(appDelegate->globalConnection));
return -1;
}

return count;
}

然后将诊断信息放入removeSegmentWithSegmentId:

- (BOOL)removeSegmentWithSegmentId:(NSInteger)sId
{
BOOL success = NO;
NSInteger count = [self countSegmentWithSegmentId:sId];

NSLog(@"%s there are %d records with segment_id of %d", __FUNCTION__, count, sId);

AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];

sqlite3_stmt *statement;

NSString *removeKeyword = [NSString stringWithFormat:@"DELETE FROM segment WHERE segment.segment_id = %d",sId];

if (sqlite3_prepare_v2(appDelegate->globalConnection, [removeKeyword UTF8String], -1, &statement, NULL) == SQLITE_OK)
{
if(sqlite3_step(statement) == SQLITE_DONE)
{
success = YES;
}
else
{
NSLog(@"%s: step not ok: %s", __FUNCTION__, sqlite3_errmsg(appDelegate->globalConnection));
}
sqlite3_finalize(statement);
}
else
{
NSLog(@"%s: prepare failure: %s", __FUNCTION__, sqlite3_errmsg(appDelegate->globalConnection));
}

return success;
}

关于iphone - iOS Sqlite3 删除查询不删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13578218/

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