2

我正在使用此代码从数据库中为我的 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;
}

但它不起作用,有人可以指导我吗?

4

4 回答 4

8

你的方法返回YES吗?

有几件事:

  • 始终登录sqlite3_errmsg任何故障
  • 现在,您所做sqlite3_finalize的只是sqlite3_stepreturn 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;
}
于 2012-11-27T06:26:26.050 回答
2
if(sqlite3_step(statement) == SQLITE_DONE) 
{
sqlite3_finalize(statement);
            return YES;
}
else
{
NSLog(@"Failed to delete row %s", sqlite3_errmsg(database));
}

检查错误消息。

于 2012-11-27T06:25:26.867 回答
1

请试试这个

步骤是

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;
}
于 2012-11-27T07:17:44.070 回答
0

使用文本框中的名称删除行。

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

    if (sqlite3_open(dbpath, &database) == SQLITE_OK) { NSString *querySQL = [NSString stringWithFormat: @"DELETE FROM person WHERE name='%@'",name]; 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_close(数据库);返回否;}

删除成功返回Yes,删除失败返回NO。

于 2018-01-10T12:18:22.867 回答