Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone iOS Sqlite3删除查询不删除行_Iphone_Objective C_Ios_Sqlite - Fatal编程技术网

Iphone iOS Sqlite3删除查询不删除行

Iphone iOS Sqlite3删除查询不删除行,iphone,objective-c,ios,sqlite,Iphone,Objective C,Ios,Sqlite,我正在使用此代码从数据库中为我的ipad应用程序删除一行 -(BOOL) removeSegmentWithSegmentId:(NSInteger)sId { AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate]; sqlite3_stmt *statement; NSString *removeKeyword =[NSString string

我正在使用此代码从数据库中为我的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;
}
但它不起作用,有人能给我指点一下吗

if(sqlite3_step(statement) == SQLITE_DONE) 
{
sqlite3_finalize(statement);
            return YES;
}
else
{
NSLog(@"Failed to delete row %s", sqlite3_errmsg(database));
}

检查错误消息。

您的方法是否返回YES

有几件事:

  • 始终记录所有故障的
    sqlite3\u errmsg
  • 现在,您只需要执行
    sqlite3\u finalize
    sqlite3\u步骤
    返回
    SQLITE\u DONE
    ,而只要您成功执行
    sqlite3\u prepare\u 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;
}
然后将诊断消息放入带有段ID的
删除段中:

- (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;
}
请试试这个

步骤是

1.开放式数据库

2.从表中删除行

3.关闭数据库

还添加了NSLog以在控制台中查看错误

//------------------------------------------------------------------------
// Method : checkAndCreateDatabase
// Method to Check and Create the database
//------------------------------------------------------------------------
//Function to check & create a database
-(void) checkAndCreateDatabase
{
    //-------------------------------------------
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    success = [fileManager fileExistsAtPath:cDatabasePath];
    //-------------------------------------------
    //databse already there
    if(success)
    {
        return;
    }
    //-------------------------------------------
    //create database
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:cDatabaseName];
    [fileManager copyItemAtPath:databasePathFromApp toPath:cDatabasePath error:nil];

}

//------------------------------------------------------------------------
// Method : checkAndCreateDatabase
// Method to open database
//------------------------------------------------------------------------
-(void) openDatabase
{

        cDatabaseName = @"db.sqlite";

    NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDir = [documentsPaths objectAtIndex:0];
    cDatabasePath = [documentDir stringByAppendingPathComponent:cDatabaseName];
    [self checkAndCreateDatabase];
    //-------------------------------------------
    if(sqlite3_open([cDatabasePath UTF8String],&database) == SQLITE_OK)
    {
      //nothing
    }
}

//------------------------------------------------------------------------
// Method : closeDatabase
// Method to close database
//------------------------------------------------------------------------
- (void)closeDatabase
{

    // Close the database.
    if (sqlite3_close(database) != SQLITE_OK) {
        //NSLog(@"Error: failed to close database with message '%s'.", sqlite3_errmsg(database));

    }


}


-(BOOL) removeSegmentWithSegmentId:(NSInteger)sId
{
    //for sharing variables of appdelegate file
    AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];

    BOOL isDeleted=NO;
    [self openDatabase];
    const char *sqlStatement;
    sqlStatement = "DELETE FROM segment WHERE segment_id =?";
    sqlite3_stmt *compiledStatement;

    if(sqlite3_prepare_v2(appDelegate.database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
    {   
        sqlite3_bind_int(compiledStatement, 1, sId); 

        if(SQLITE_DONE != sqlite3_step(compiledStatement))
        {
            NSLog( @"Error while deleting metadata of  segment '%s'", sqlite3_errmsg(appDelegate.database));         

        }
        else 
        {
            NSLog(@"Deleted chart segment successfully !");
            isDeleted=YES;

        }
        //-------------------------------------------
        sqlite3_reset(compiledStatement);       
    }
    else 
    {
        NSLog( @"Error while deleting segment of  chart '%s'", sqlite3_errmsg(appDelegate.database));

    }
    sqlite3_finalize(compiledStatement);
    [self closeDatabase];
    return isDeleted;
}
从文本框中删除使用名称的行

  • (BOOL)deleteRow:(NSString*)名称{ const char*dbpath=[databasePath UTF8String]

    if (sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL) == SQLITE_OK) {
        if (sqlite3_step(statement) == SQLITE_DONE) {
            sqlite3_finalize(statement);
            sqlite3_close(database);
            return YES;
        }
        else
        {
            NSLog(@"%d",sqlite3_step(statement));
        }
    }
    sqlite3_finalize(statement);
    
    if(sqlite3\u打开(dbpath和数据库)==SQLITE\u正常){ NSString*querySQL=[NSString stringWithFormat:@“从姓名='%@',姓名]的人处删除”; const char*query_stmt=[querySQL UTF8String]

    if (sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL) == SQLITE_OK) {
        if (sqlite3_step(statement) == SQLITE_DONE) {
            sqlite3_finalize(statement);
            sqlite3_close(database);
            return YES;
        }
        else
        {
            NSLog(@"%d",sqlite3_step(statement));
        }
    }
    sqlite3_finalize(statement);
    
    } sqlite3_关闭(数据库); 返回否; }

如果删除成功,则返回是;如果删除失败,则返回否


我打赌您正在尝试从驻留在应用程序包中的数据库中删除…不,情况并非如此。感谢您提供此代码片段,它可能会提供一些有限的即时帮助。A通过展示为什么这是一个很好的解决问题的方法,并将使它对未来有其他类似问题的读者更有用。请在您的回答中添加一些解释,包括您所做的假设。