[英]SQLite iOS warning : Comparison of constant 101 with expression of type BOOL is always false
I have downloaded sample code for SQLite learning. 我已经下载了SQLite学习的示例代码。 I'm using Xcode 6.1.1 and iPhone 6 plus simulator. 我正在使用Xcode 6.1.1和iPhone 6 plus模拟器。 After running app on simulator I'm getting DB Error : unknown error
from query execution. 在模拟器上运行应用程序后,我收到DB Error : unknown error
查询执行时DB Error : unknown error
。 Below is part of code where I'm getting warning as Comparison of constant 101 with expression of type 'BOOL' (aka 'bool') is always false.
下面是我收到警告的代码的一部分,因为Comparison of constant 101 with expression of type 'BOOL' (aka 'bool') is always false.
// Execute the query.
BOOL executeQueryResults = sqlite3_step(compiledStatement);
if (executeQueryResults == SQLITE_DONE) {
// Keep the affected rows.
self.affectedRows = sqlite3_changes(sqlite3Database);
// Keep the last inserted row ID.
self.lastInsertedRowID = sqlite3_last_insert_rowid(sqlite3Database);
}
else {
// If could not execute the query show the error message on the debugger.
NSLog(@"DB Error: %s", sqlite3_errmsg(sqlite3Database));
}
What might be fix for this? 可能有什么办法解决这个问题?
Checking condition for compiledStatement directly solved the problem : 对于compiledStatement检查条件直接解决了这个问题:
// Execute the query.
// BOOL executeQueryResults = sqlite3_step(compiledStatement);
// if (executeQueryResults == SQLITE_DONE) {
if (sqlite3_step(compiledStatement)) {
// Keep the affected rows.
self.affectedRows = sqlite3_changes(sqlite3Database);
// Keep the last inserted row ID.
self.lastInsertedRowID = sqlite3_last_insert_rowid(sqlite3Database);
}
else {
// If could not execute the query show the error message on the debugger.
NSLog(@"DB Error: %s", sqlite3_errmsg(sqlite3Database));
}
Try changing Your code 尝试更改您的代码
if (sqlite3_step(compiledStatement) == SQLITE_DONE) {
// Keep the affected rows.
self.affectedRows = sqlite3_changes(sqlite3Database);
// Keep the last inserted row ID.
self.lastInsertedRowID = sqlite3_last_insert_rowid(sqlite3Database);
}
else {
// If could not execute the query show the error message on the debugger.
NSLog(@"DB Error: %s", sqlite3_errmsg(sqlite3Database));
}
That's beacause TRUE
is probaly 1 or -1, but never 101 (SQLITE_DONE = 101). 这是因为TRUE
是probaly 1或-1,但从不101(SQLITE_DONE = 101)。 Boolean values are true
and false
but never SQLITE_DONE
. 布尔值为true
和false
但从不是SQLITE_DONE
。
You could write something like 你可以写点像
bool done = sqlite3_step(compiledStatement) == SQLITE_DONE;
if (done) {
...
}
But if don't need this result later, there is no point in defining this variable at all 但是如果以后不需要这个结果,根本就没有必要定义这个变量
if (sqlite3_step(compiledStatement) == SQLITE_DONE) {
...
}
I know it's late,but it can help someone in future . 我知道它已经很晚了,但它可以帮助将来的某个人。
// This is the case of an executable query (insert, update, ...).
// Execute the query.
BOOL executeQueryResults = sqlite3_step(compiledStatement);
if (executeQueryResults) {
// Keep the affected rows.
self.affectedRows = sqlite3_changes(sqlite3Database);
NSLog(@"affected row = %d",self.affectedRows);
// Keep the last inserted row ID.
self.lastInsertedRowID = sqlite3_last_insert_rowid(sqlite3Database);
NSLog(@"last iserted row = %lld",self.lastInsertedRowID);
}
else {
// If could not execute the query show the error message on the debugger.
NSLog(@"DB Error: %s", sqlite3_errmsg(sqlite3Database));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.