SQL ラッパー (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 は一意のコードです。
誰かがこの問題についてアドバイスをくれますか? ありがとうございました!