gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:31:46 26 4
gpt4 key购买 nike

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

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

最佳答案

我认为问题在于您的更新语句。传递给“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
];

关于iphone FMDB/SQLite 更新查询不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12621073/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com