diff --git a/src/SQLiteC++/Database.cpp b/src/SQLiteC++/Database.cpp index 614ec88..be73c7f 100644 --- a/src/SQLiteC++/Database.cpp +++ b/src/SQLiteC++/Database.cpp @@ -67,5 +67,23 @@ void Database::unregisterStatement(Statement& aStatement) // throw(SQLite::Excep } } +// Shorcut to execute one or multiple SQL statements without results. +int Database::exec(const char* apQueries) // throw(SQLite::Exception); +{ + int ret = sqlite3_exec(mpSQLite, apQueries, NULL, NULL, NULL); + check(ret); + + // Return the number of changes made by thoses SQL statements + return sqlite3_changes(mpSQLite); +} + +// Check if aRet equal SQLITE_OK, else throw a SQLite::Exception with the SQLite error message +void Database::check(const int aRet) const // throw(SQLite::Exception) +{ + if (SQLITE_OK != aRet) + { + throw SQLite::Exception(sqlite3_errmsg(mpSQLite)); + } +} }; // namespace SQLite diff --git a/src/SQLiteC++/Database.h b/src/SQLiteC++/Database.h index 26efc0f..57cac2f 100644 --- a/src/SQLiteC++/Database.h +++ b/src/SQLiteC++/Database.h @@ -68,6 +68,17 @@ public: */ void unregisterStatement(Statement& aStatement); // throw(SQLite::Exception); + /** + * @brief Shorcut to execute one or multiple statements without results. + * + * This is usefull for Data Manipulation Language SQL statements like CREATE, INSERT, UPDATE, DROP + * + * @see also Statement class for handling queries with resultats + * + * @param[in] apQueries one or multiple UTF-8 encoded, semicolon-separate SQL statements + */ + int exec(const char* apQueries); // throw(SQLite::Exception); + /** * @brief Filename used to open the database */ @@ -90,6 +101,11 @@ private: Database(const Database&); Database& operator=(const Database&); + /** + * @brief Check if aRet equal SQLITE_OK, else throw a SQLite::Exception with the SQLite error message + */ + void check(const int aRet) const; // throw(SQLite::Exception); + private: sqlite3* mpSQLite; //!< Pointer to SQLite Database Connection Handle std::string mFilename; //!< UTF-8 filename used to open the database diff --git a/src/SQLiteC++/Statement.cpp b/src/SQLiteC++/Statement.cpp index 5626a1e..2679cc9 100644 --- a/src/SQLiteC++/Statement.cpp +++ b/src/SQLiteC++/Statement.cpp @@ -147,10 +147,7 @@ bool Statement::isColumnNull(const int aIndex) const // throw(SQLite::Exception) return (SQLITE_NULL == sqlite3_column_type(mpStmt, aIndex)); } - -/** - * @brief Check if aRet equal SQLITE_OK, else throw a SQLite::Exception with the SQLite error message - */ +// @brief Check if aRet equal SQLITE_OK, else throw a SQLite::Exception with the SQLite error message void Statement::check(const int aRet) const // throw(SQLite::Exception) { if (SQLITE_OK != aRet) @@ -159,6 +156,7 @@ void Statement::check(const int aRet) const // throw(SQLite::Exception) } } + //////////////////////////////////////////////////////////////////////////////// // Implementation of the inner class Statement::Column // diff --git a/src/example1/main.cpp b/src/example1/main.cpp index e4855da..02bff37 100644 --- a/src/example1/main.cpp +++ b/src/example1/main.cpp @@ -55,7 +55,7 @@ private: int main (void) { - // Basic example (1/2) : + // Basic example (1/3) : try { // Open a database file @@ -63,7 +63,7 @@ int main (void) std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n"; // Compile a SQL query, containing one parameter (index 1) - SQLite::Statement query(db, "SELECT * FROM test WHERE size > ?"); + SQLite::Statement query(db, "SELECT * FROM test WHERE size > ?;SELECT id FROM test"); std::cout << "SQLite statement '" << query.getQuery().c_str() << "' compiled (" << query.getColumnCount () << " columns in the result)\n"; // Bind the integer value 6 to the first parameter of the SQL query query.bind(1, 6); @@ -98,7 +98,7 @@ int main (void) } //////////////////////////////////////////////////////////////////////////// - // Object Oriented Basic example (2/2) : + // Object Oriented Basic example (2/3) : try { // Open the database and compile the query @@ -114,5 +114,29 @@ int main (void) std::cout << "SQLite exception: " << e.what() << std::endl; } + //////////////////////////////////////////////////////////////////////////// + // Simple batch queries example (3/3) : + try + { + // Open a database file + SQLite::Database db("test.db3", SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); + std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n"; + + db.exec("DROP TABLE IF EXISTS test"); + + db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)"); + + int nb = db.exec("INSERT INTO test VALUES (NULL, \"test\")"); + std::cout << "INSERT INTO test VALUES (NULL, \"test\")\", returned " << nb << std::endl; + + db.exec("DROP TABLE test"); + } + catch (std::exception& e) + { + std::cout << "SQLite exception: " << e.what() << std::endl; + } + std::remove("test.db3"); + + return 0; }