【发布时间】:2013-11-08 06:02:39
【问题描述】:
我正在更新 sqlite 表中的列,它工作正常,但我认为当每次调用方法来更新值时,我认为如果有一条记录,则将更新值混合到其他记录,那么如果我们有两条记录就可以了,然后我觉得这里搞混了是更新代码
- (void)addContentViews:(NSString *)dbPath {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"The Content ID is %@",appDelegate.contentID);
//if(updateStmt == nil) {
NSLog(@"The Content ID is %@",appDelegate.contentID);
NSString*select = [NSString stringWithFormat:@"UPDATE ContentMaster Set Views = ? where contentID ='%@'",appDelegate.contentID];
const char *sql = [select UTF8String];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK)
NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
// }
sqlite3_bind_text(updateStmt,1, [views UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(updateStmt))
NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
//else
//SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
// contentID = sqlite3_last_insert_rowid(database);
//Reset the add statement.
sqlite3_finalize(updateStmt);
}
}
【问题讨论】:
-
不相关,但是 (a) 你调用了
sqlite3_reset(这是为了重置准备好的语句,以便你可以绑定新值并再次调用sqlite3_step)我怀疑你的意图是sqlite3_finalize(其中用于释放sqlite3_prepare_v2) 期间分配的内存; (b) 您可能希望从准备到最终确定的所有内容都位于成功打开的if子句中(即,将所有内容都包裹在大括号内)。现在,如果打开失败,您将跳过准备,但您仍将尝试绑定、步进和完成; (c) 你的意思是关闭数据库吗? -
@Rob 你能在我的代码中编辑一下,这样我就可以理解使用什么谢谢
-
我已经修改了你的问题,将
sqlite3_reset替换为sqlite3_finalize,并在周围添加了缺少的大括号,因此if数据库打开失败,它不仅没有准备好,而且没有绑定、步骤或最终确定。我还修复了 step 命令的if语句,这样如果出现错误,它仍然会完成准备好的语句。 -
为什么不使用 Fmdb 进行 Sqlite 操作?
标签: ios iphone sqlite sql-update