我尝试使用 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 是唯一代码;
有人可以帮我就这个问题提供一些建议吗?谢谢!