gpt4 book ai didi

iphone - SQLite3 更新不工作

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

我正在尝试对我的 sqlite3 表执行更新命令。但它不起作用:

+(void)updateContact:(Contact *)c withOriginalFirst:(NSString *)originalFirst originalLast:(NSString *)originalLast originalBriefDescription:(NSString *)originalBriefDescription {

sqlite3 *database;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

NSString *cmd = [NSString stringWithFormat:@"update contacts set first='%@', last='%@', briefDescription='%@' where first='%@' and last='%@' and briefDescription='%@';",
[c first],[c last],[c briefDescription],originalFirst,originalLast,originalBriefDescription];
const char * sql = [cmd UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sql, -1, &compiledStatement, NULL) == SQLITE_OK) {
NSLog(@"updateContact SUCCESS - executed command %@",cmd);
}
else {
NSLog(@"updateContact FAILED - failed to execute command %@",cmd);
}

sqlite3_finalize(compiledStatement);

}
else {
NSLog(@"pdateContact FAILED - failed to open database");
}

sqlite3_close(database);

NSLog(@"After update, contacts = %@",[SQLMaster getContactsFromDatabase]);

}

我看到打印了“updateContact SUCCESS - executed command ...”。但是表格没有更新。我做错了什么?

最佳答案

一些事情:

你的数据库路径指向哪里?确保它指向包中的文档路径,而不是直接指向项目中的外部数据库。另外,请确保正确放置步骤语句。

+(void)updateContact:(Contact *)c withOriginalFirst:(NSString *)originalFirst originalLast:(NSString *)originalLast originalBriefDescription:(NSString *)originalBriefDescription {

sqlite3 *database;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

NSString *cmd = [NSString stringWithFormat:@"update contacts set first='%@', last='%@', briefDescription='%@' where first='%@' and last='%@' and briefDescription='%@';",
[c first],[c last],[c briefDescription],originalFirst,originalLast,originalBriefDescription];
const char * sql = [cmd UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sql, -1, &compiledStatement, NULL) == SQLITE_OK) {
sqlite3_step(compiledStatement); // Here is the added step.
NSLog(@"updateContact SUCCESS - executed command %@",cmd);
}
else {
NSLog(@"updateContact FAILED - failed to execute command %@",cmd);
}

sqlite3_finalize(compiledStatement);

}
else {
NSLog(@"pdateContact FAILED - failed to open database");
}

sqlite3_close(database);
NSLog(@"After update, contacts = %@",[SQLMaster getContactsFromDatabase]);

}

此外,我从 [SQLMaster getContactsFromDatabase] 行看到您正在调用其他数据库方法。确保所有这些数据库调用都达到了 sqlite3_finalize,否则数据库可能不会释放繁忙的处理程序。

关于iphone - SQLite3 更新不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6514187/

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