我已经下载了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.
// 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));
}
可能有什么办法解决这个问题?
答案 0 :(得分: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));
}
答案 1 :(得分: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));
}
答案 2 :(得分:0)
因为TRUE
可能是1或-1,但从不101(SQLITE_DONE = 101)。布尔值为true
和false
,但绝不是SQLITE_DONE
。
你可以写点像
bool done = sqlite3_step(compiledStatement) == SQLITE_DONE;
if (done) {
...
}
但是如果以后不需要这个结果,那么在所有
中定义这个变量是没有意义的if (sqlite3_step(compiledStatement) == SQLITE_DONE) {
...
}
答案 3 :(得分: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));
}