Sou*_*jee 2 sqlite iphone ios xcode4
我正在创建一个应用程序,并使用 sqlite 进行更新。下面是我的一段代码:
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"appforoffice.sqlite"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if(!(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK))
{
NSLog(@"An error has occured.");
}
const char *sql = "UPDATE settings SET `value`='Off' WHERE `type`=?";
if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) == SQLITE_OK)
{
if(sqlite3_bind_text(updateStmt, 1, [updateType UTF8String], -1, SQLITE_TRANSIENT) == SQLITE_OK)
{
if(sqlite3_step(updateStmt) == SQLITE_DONE) {
NSLog(@"Update Done successfuly");
}
else {
NSLog(@"Error while updating. '%s'", sqlite3_errmsg(database));
}
sqlite3_finalize(updateStmt);
sqlite3_close(database);
}
else
{
NSLog(@"Error while binding variable. '%s'", sqlite3_errmsg(database));
}
}
else
{
NSLog(@"Error while creating update statement. '%s'", sqlite3_errmsg(database));
}
Run Code Online (Sandbox Code Playgroud)
但是伙计们,我没有收到任何错误。但问题是,数据库表不受查询的影响。我确信查询完全没问题。
我对这个问题感到困惑,找不到任何方法来摆脱这个问题。
sqlite3_prepare_v2()不返回布尔值,因此测试其返回值!是错误的。来自参考:
成功时,sqlite3_prepare() 系列例程返回 SQLITE_OK;否则返回错误代码。
所以你的代码应该看起来更像这样:
if (sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) == SQLITE_OK)
{
if (sqlite3_bind_text(updateStmt, 1, [updateType UTF8String], -1,
SQLITE_TRANSIENT) == SQLITE_OK)
{
... call update API ...
}
else
{
NSLog(@"Error while binding variable. '%s'", sqlite3_errmsg(database));
}
}
else
{
NSLog(@"Error while creating update statement. '%s'", sqlite3_errmsg(database));
}
Run Code Online (Sandbox Code Playgroud)
编辑(在使用整个查询更新问题后):
我不喜欢查询中 ` 字符的外观;删除它们,它应该可以工作。
| 归档时间: |
|
| 查看次数: |
11423 次 |
| 最近记录: |