From b7b440de5d8b835195627da61ba267e3b4359dbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Rombauts?= Date: Tue, 10 Nov 2015 18:32:24 +0100 Subject: [PATCH] Fix #73 Wrong exception thrown by Database constructor - switched from sqlite3_errmsg() to sqlite3_errstr() where applicable --- include/SQLiteCpp/Database.h | 2 +- include/SQLiteCpp/Statement.h | 2 +- src/Backup.cpp | 6 +----- src/Database.cpp | 6 +++--- src/Statement.cpp | 6 +++--- 5 files changed, 9 insertions(+), 13 deletions(-) diff --git a/include/SQLiteCpp/Database.h b/include/SQLiteCpp/Database.h index 71ebc5bd..7a2c1863 100644 --- a/include/SQLiteCpp/Database.h +++ b/include/SQLiteCpp/Database.h @@ -378,7 +378,7 @@ class Database { if (SQLITE_OK != aRet) { - throw SQLite::Exception(sqlite3_errmsg(mpSQLite)); + throw SQLite::Exception(sqlite3_errstr(aRet)); } } diff --git a/include/SQLiteCpp/Statement.h b/include/SQLiteCpp/Statement.h index fd081376..363e98c0 100644 --- a/include/SQLiteCpp/Statement.h +++ b/include/SQLiteCpp/Statement.h @@ -457,7 +457,7 @@ class Statement { if (SQLITE_OK != aRet) { - throw SQLite::Exception(sqlite3_errmsg(mStmtPtr)); + throw SQLite::Exception(sqlite3_errstr(aRet)); } } diff --git a/src/Backup.cpp b/src/Backup.cpp index 504042f7..ba8815a4 100644 --- a/src/Backup.cpp +++ b/src/Backup.cpp @@ -85,11 +85,7 @@ int Backup::executeStep(const int aNumPage /* = -1 */) if (SQLITE_OK != res && SQLITE_DONE != res && SQLITE_BUSY != res && SQLITE_LOCKED != res) { - std::string strerr("Backup executeStep error"); -#if SQLITE_VERSION_NUMBER >= 3007015 // SQLite v3.7.15 is the first version with sqlite3_errstr() interface - strerr += "with error message "; - strerr += sqlite3_errstr(res); -#endif + std::string strerr = sqlite3_errstr(res); throw SQLite::Exception(strerr); } return res; diff --git a/src/Database.cpp b/src/Database.cpp index b9440ac2..2ce2183c 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -36,7 +36,7 @@ Database::Database(const char* apFilename, const int ret = sqlite3_open_v2(apFilename, &mpSQLite, aFlags, apVfs); if (SQLITE_OK != ret) { - std::string strerr = sqlite3_errmsg(mpSQLite); + std::string strerr = sqlite3_errstr(ret); sqlite3_close(mpSQLite); // close is required even in case of error on opening throw SQLite::Exception(strerr); } @@ -58,7 +58,7 @@ Database::Database(const std::string& aFilename, const int ret = sqlite3_open_v2(aFilename.c_str(), &mpSQLite, aFlags, aVfs.empty() ? NULL : aVfs.c_str()); if (SQLITE_OK != ret) { - std::string strerr = sqlite3_errmsg(mpSQLite); + std::string strerr = sqlite3_errstr(ret); sqlite3_close(mpSQLite); // close is required even in case of error on opening throw SQLite::Exception(strerr); } @@ -85,7 +85,7 @@ Database::~Database() noexcept // nothrow /** * @brief Set a busy handler that sleeps for a specified amount of time when a table is locked. * - * This is usefull in multithreaded program to handle case where a table is locked for writting by a thread. + * This is useful in multithreaded program to handle case where a table is locked for writting by a thread. * Any other thread cannot access the table and will receive a SQLITE_BUSY error: * setting a timeout will wait and retry up to the time specified before returning this SQLITE_BUSY error. * Reading the value of timeout for current connection can be done with SQL query "PRAGMA busy_timeout;". diff --git a/src/Statement.cpp b/src/Statement.cpp index 86e59195..519daf5d 100644 --- a/src/Statement.cpp +++ b/src/Statement.cpp @@ -195,7 +195,7 @@ bool Statement::executeStep() { mbOk = false; mbDone = false; - throw SQLite::Exception(sqlite3_errmsg(mStmtPtr)); + throw SQLite::Exception(sqlite3_errstr(ret)); } } else @@ -227,7 +227,7 @@ int Statement::exec() { mbOk = false; mbDone = false; - throw SQLite::Exception(sqlite3_errmsg(mStmtPtr)); + throw SQLite::Exception(sqlite3_errstr(ret)); } } else @@ -317,7 +317,7 @@ Statement::Ptr::Ptr(sqlite3* apSQLite, std::string& aQuery) : const int ret = sqlite3_prepare_v2(apSQLite, aQuery.c_str(), static_cast(aQuery.size()), &mpStmt, NULL); if (SQLITE_OK != ret) { - throw SQLite::Exception(sqlite3_errmsg(mpSQLite)); + throw SQLite::Exception(sqlite3_errstr(ret)); } // Initialize the reference counter of the sqlite3_stmt : // used to share the mStmtPtr between Statement and Column objects;