2

我尝试使用 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 是唯一代码;

    有人可以帮我就这个问题提供一些建议吗?谢谢!

4

2 回答 2

3

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

在您的更新 SQL 语句中,entities.debt_code并且entities.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
          ];
于 2012-09-27T12:26:07.700 回答
2

把你的更新 SQL 放到一个 NSString 中,这样你就可以在 NSLog 中输出它,看看它是否有问题。还要检查 FMDatabase 对象上的 lastError 属性 - 如果它不为零,则 NSLog 同一对象的 lastErrorMessage 属性以查看 FMDB 抱怨的内容。

于 2012-09-27T12:15:16.830 回答