2014-12-26 44 views
4

我已經下載了SQLite學習的示例代碼。我使用Xcode 6.1.1和iPhone 6 plus模擬器。在模擬器上運行應用後,我從查詢執行中獲得DB Error : unknown error。下面是我得到警告的代碼的一部分Comparison of constant 101 with expression of type 'BOOL' (aka 'bool') is always false.SQLite iOS警告:常量101與BOOL類型表達式的比較始終爲假

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

有什麼可以解決這個問題?

Screenshot for warning

+0

將類型從BOOL更改爲int。 – manecosta

+3

您不必在此創建單獨的變量'executeQueryResults'。在條件中直接使用'sqlite3_step(compiledStatement)'。另外'sqlite3_step(compiledStatement);'返回類型是'int'而不是'BOOL'。 – Kampai

回答

9

檢查條件定義該變量爲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)); 
} 
4

試着改變你的代碼

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)); 
    } 
0

這是東陽TRUE是probaly 1或-1,但從來沒有101(SQLITE_DONE = 101)。布爾值是truefalse但從不SQLITE_DONE

你可以寫類似

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

但是,如果不需要這個結果後,沒有在所有

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

我知道這很晚,但它可以幫助未來的人。

// 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)); 
       } 
相關問題