[英]I am getting an error with sqlite3_prepare_v2
I am creating an app, and doing update with sqlite. 我正在创建一个应用程序,并使用sqlite进行更新。 Here is my piece of code given below: 这是我下面的代码:
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));
}
But guys, I am not getting any error . 但是,伙计们, 我没有任何错误 。 But the problem is that, the database table is not being effected by the query . 但是问题在于, 数据库表不受查询影响 。 I am sure the query is perfectly alright. 我确定查询完全正确。
I am confused about the problem, can't find any way to get out of this. 我对这个问题感到困惑,找不到任何解决方法。
sqlite3_prepare_v2()
doesn't return boolean, so testing its return value with !
sqlite3_prepare_v2()
不返回布尔值,因此使用!
测试其返回值!
is wrong. 是错的。 From the reference : 从参考 :
On success, the sqlite3_prepare() family of routines return SQLITE_OK; 成功后,sqlite3_prepare()例程系列将返回SQLITE_OK;否则,将返回SQLITE_OK。 otherwise an error code is returned. 否则返回错误代码。
So your code should look more like this: 因此,您的代码应更像这样:
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));
}
EDIT (after the question was updated with the whole query): 编辑 (用整个查询更新问题之后):
I don't like the look of the ` characters in the query; 我不喜欢查询中`字符的外观; remove them and it should work. 删除它们,它应该可以工作。
I prefer to use sql command straight forward without any prepare or bind Methods 我更喜欢直接使用sql命令,而无需任何准备或绑定方法
sqlite3_exec(database,[sql UTF8String],nil,nil,nil);
where sql is an NSString which has the update statement ,Just make sure your db is open 其中sql是具有update语句的NSString,只需确保您的数据库已打开
hope that helps 希望能有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.