gpt4 book ai didi

objective-c - sqlite数据库更新

转载 作者:行者123 更新时间:2023-12-03 18:39:59 24 4
gpt4 key购买 nike

我正在使用以下代码在sqlite中更新表中的条目。不干扰其他列,只需要编辑余额之一即可。但是它没有给我数据库上的任何更改。有人可以在这里帮助我吗..

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"Accounts.sqlite"];

sqlite3 *database;
sqlite3_stmt *updateStmt;
if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK)
{
const char *sql = "update Account Set currentBalance = ? Where ID = ?";
if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK)
NSAssert1(0, @"Error while creating update statement. '%s'", sqlite3_errmsg(database));
}
sqlite3_bind_int(updateStmt, 1, 1000);

sqlite3_bind_int(updateStmt, 2 , 1);
sqlite3_finalize(updateStmt);


if(SQLITE_DONE != sqlite3_step(updateStmt))
NSAssert1(0, @"Error while updating. '%s'", sqlite3_errmsg(database));
sqlite3_finalize(updateStmt);


sqlite3_close(database);

最佳答案

做这个...

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"Accounts.sqlite"];
//NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Accounts.sqlite"];
sqlite3 *database;
sqlite3_stmt *updateStmt;
if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK)
{
const char *sql = "update Account Set currentBalance = ? Where ID = ?";
if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK)
NSLog(@"Error while creating update statement. %s", sqlite3_errmsg(database));
}
sqlite3_bind_int(updateStmt, 1, 1000);

sqlite3_bind_int(updateStmt, 2 , 1);


char* errmsg;
sqlite3_exec(database, "COMMIT", NULL, NULL, &errmsg);

if(SQLITE_DONE != sqlite3_step(updateStmt))
NSLog(@"Error while updating. %s", sqlite3_errmsg(database));
sqlite3_finalize(updateStmt);


sqlite3_close(database);

关于objective-c - sqlite数据库更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7169966/

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