简体   繁体   English

SQLite iOS警告:常量101与BOOL类型表达式的比较始终为false

[英]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 . 布尔值为truefalse但从不是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.

相关问题 iOS7与常量和表达式的比较总是假的 - iOS7 comparison of constant with expression is always false 常量000000000000与类型'NSInteger'(aka'int')的表达式的比较始终为false - Comparison of constant 10000000000 with expression of type 'NSInteger' (aka 'int') is always false 随着新的XCode得到“常量与布尔表达式的比较始终为假” - With new XCode getting “Comparison of constant with boolean expression is always false” 在iOS 8.1.1中,typecast BOOL始终返回false - typecast BOOL always returns false in iOS 8.1.1 意外的类型名称“ BOOL”:iOS SDK 6.1(设备)中的预期表达式 - Unexpected type name 'BOOL': expected expression in iOS SDK 6.1 (Device) 表达式不是iOS中的整数常量表达式 - Expression is not an integer constant expression in iOS 无符号表达式的比较> = 0始终为真 - Comparison of unsigned expression >=0 is always true 在共享NSUserDefaults中存储布尔值始终返回false - Storing bool in shared NSUserDefaults always returns false UITableViewCell setHighlighted:突出显示的(BOOL)始终为false - UITableViewCell setHighlighted:(BOOL)highlighted is always false 来自 iOS 应用的拨号号码:无法将表达式的类型“Bool”转换为“NSURL!” - Dial number from iOS app: Cannot convert the expression's type 'Bool' to type 'NSURL!'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM