SQLite iOS warning: comparing constant 101 with a BOOL expression is always incorrect

I downloaded the sample code for training SQLite. I am using Xcode 6.1.1 and iPhone 6 plus a simulator. After starting the application on the simulator, I get DB Error : unknown errorfrom the execution of the request. Below is the piece of code where I get a warning likeComparison 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 could be the reason for this?

Screenshothot for warning

+4
source share
4 answers

The validation condition for compiledStatement directly solved the problem:

// 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));
 }
+9
source

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));
  }
+4
source

beacause TRUE 1 -1, 101 (SQLITE_DONE = 101). TRUE false, SQLITE_DONE.

-

bool done = sqlite3_step(compiledStatement) == SQLITE_DONE;
if (done) {
    ...
}

But if it is not needed later, it makes no sense to define this variable at all

if (sqlite3_step(compiledStatement) == SQLITE_DONE) {
    ...
}
0
source

I know this late, but it may help someone in the 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));
                }
0
source

Source: https://habr.com/ru/post/1569048/


All Articles