iOS Sqlite3删除查询不删除行

时间:2012-11-27 06:00:06

标签: iphone objective-c ios sqlite

我正在使用此代码从我的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 个答案:

答案 0 :(得分:8)

您的方法是否返回YES?

有几件事:

  • 始终记录任何失败的sqlite3_errmsg
  • 现在,您只是sqlite3_finalize sqlite3_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;
}

答案 1 :(得分:2)

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

检查错误消息。

答案 2 :(得分: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;
}

答案 3 :(得分: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(数据库); 返回NO; }

  

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