iphone FMDB / SQLite更新查询不起作用

时间:2012-09-27 12:10:36

标签: iphone ios sqlite sql-update fmdb

我尝试使用sql wrapper(FMDB)在表中进行更新。插入方法很明显,当我尝试更新表中的数据时会出现问题。

- (void)saveUserMoneyValidation:(UserDebtInfo *)entities
{
    NSLog(@"entities.client_index = %d", entities.client_index);

    [self deleteUserMoneyValidation:entities.client_index];

    FMDatabase *newdb = [FMDatabase databaseWithPath:databasePathStore];

    [newdb setLogsErrors:TRUE];
    [newdb setTraceExecution:FALSE];

    if (![newdb open])
    { 
        NSLog(@"!!! Could not open db."); 
        return; 
    }
    else 
    { 
        NSLog(@"DB Open successfuly."); 
    }

    [newdb beginTransaction];
    NSLog(@"entities = %@",entities);

    [newdb executeUpdate:@"INSERT INTO MoneyValidation (client_index, client_name, sum, duration, monthly_payment, payment_type, debt_type, debt_code, categoryType) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
    [NSNumber numberWithInt:entities.client_index],
     entities.client_name,
     entities.sum,
     entities.duration,
     entities.monthly_payment,
     entities.payment_type,
     entities.debt_type,
     [NSNumber numberWithInt:entities.debt_code],
     entities.categoryType
     ];

    [newdb commit];
    [newdb close];
}

更新方法:

   -(void)updateUserMoneyValidation:(UserDebtInfo *)entities{
NSLog(@"entities.client_index = %d", entities.client_index);

FMDatabase *newdb = [FMDatabase databaseWithPath:databasePathStore];

[newdb setLogsErrors:TRUE];
[newdb setTraceExecution:FALSE];

if (![newdb open])
{ 
    NSLog(@"!!! Could not open db."); 
    return; 
}
else 
{ 
    NSLog(@"DB Open successfuly."); 
}

BOOL success = NO;

[newdb beginTransaction];
NSLog(@"entities = %@",entities);

success = [newdb executeUpdate:@"UPDATE MoneyValidation SET client_name = ?, sum = ?, duration = ?, monthly_payment = ?, payment_type = ?, debt_type = ?, debt_code = ?, categoryType = ?  WHERE client_index = %d;", entities.client_name, entities.sum, entities.duration, entities.duration, entities.monthly_payment, entities.payment_type, entities.debt_type, entities.debt_code, entities.categoryType, entities.client_index];

if (success)
{
    NSLog(@"OK");
    [db commit];
    [db close];
}
else 
{
    NSLog(@"FAIL");
}

NSLog(@"OBJ - %@", entities);}

表格结构:

CREATE TABLE MoneyValidation (ID INTEGER PRIMARY KEY, client_index INTEGER, client_name TEXT, sum TEXT, duration TEXT, monthly_payment TEXT, payment_type TEXT, debt_type TEXT, debt_code INTEGER, categoryType TEXT)
  • client_index是唯一代码;

    有人可以帮我提一些关于这个问题的建议吗? 谢谢!

2 个答案:

答案 0 :(得分:3)

我认为问题出在你的更新声明中。传递给“executeUpdate”的所有值都应该是对象。

在更新SQL语句中,entities.debt_codeentities.client_index作为INTEGERS传递,您需要将它们作为 NSNumber 对象传递。

尝试以下方法:

success = [newdb executeUpdate:@"UPDATE MoneyValidation 
           SET client_name = ?, sum = ?, 
           duration = ?, monthly_payment = ?, 
           payment_type = ?, debt_type = ?, 
           debt_code = ?, categoryType = ?  
           WHERE client_index = %d;", 

          entities.client_name, entities.sum, 
          entities.duration, entities.duration, 
          entities.monthly_payment, entities.payment_type, 
          entities.debt_type, 
          [NSNumber numberWithInt:entities.debt_code],  // Convert int to object type
          entities.categoryType, 
          [NSNumber numberWithInt:entities.client_index] // Convert int to object type
          ];

答案 1 :(得分:2)

将您的更新SQL放入NSString中,以便您可以在NSLog中输出它并查看它是否存在问题。还要检查FMDatabase对象的lastError属性 - 如果它不为零,则NSLog同一对象的lastErrorMessage属性,以查看FMDB抱怨的内容。