|
| 1 | +/** |
| 2 | + * @file main.cpp |
| 3 | + * @brief A few short examples in a row. |
| 4 | + * |
| 5 | + * Demonstrates how-to use the SQLite++ wrapper |
| 6 | + * |
| 7 | + * Copyright (c) 2012-2016 Sebastien Rombauts ([email protected]) |
| 8 | + * |
| 9 | + * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt |
| 10 | + * or copy at http://opensource.org/licenses/MIT) |
| 11 | + */ |
| 12 | + |
| 13 | +#include <iostream> |
| 14 | +#include <cstdio> |
| 15 | +#include <cstdlib> |
| 16 | + |
| 17 | +#include <SQLiteCpp/SQLiteCpp.h> |
| 18 | + |
| 19 | + |
| 20 | +#ifdef SQLITECPP_ENABLE_ASSERT_HANDLER |
| 21 | +namespace SQLite |
| 22 | +{ |
| 23 | +/// definition of the assertion handler enabled when SQLITECPP_ENABLE_ASSERT_HANDLER is defined in the project (CMakeList.txt) |
| 24 | +void assertion_failed(const char* apFile, const long apLine, const char* apFunc, const char* apExpr, const char* apMsg) |
| 25 | +{ |
| 26 | + // Print a message to the standard error output stream, and abort the program. |
| 27 | + std::cerr << apFile << ":" << apLine << ":" << " error: assertion failed (" << apExpr << ") in " << apFunc << "() with message \"" << apMsg << "\"\n"; |
| 28 | + std::abort(); |
| 29 | +} |
| 30 | +} |
| 31 | +#endif |
| 32 | + |
| 33 | +int main () |
| 34 | +{ |
| 35 | + // Using SQLITE_VERSION would require #include <sqlite3.h> which we want to avoid: use SQLite::VERSION if possible. |
| 36 | +// std::cout << "SQlite3 version " << SQLITE_VERSION << std::endl; |
| 37 | + std::cout << "SQlite3 version " << SQLite::VERSION << " (" << SQLite::getLibVersion() << ")" << std::endl; |
| 38 | + std::cout << "SQliteC++ version " << SQLITECPP_VERSION << std::endl; |
| 39 | + |
| 40 | + //////////////////////////////////////////////////////////////////////////// |
| 41 | + // Simple batch queries example : |
| 42 | + try |
| 43 | + { |
| 44 | + // Open a database file in create/write mode |
| 45 | + SQLite::Database db("test.db3", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE); |
| 46 | + std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n"; |
| 47 | + |
| 48 | + // Create a new table with an explicit "id" column aliasing the underlying rowid |
| 49 | + db.exec("DROP TABLE IF EXISTS test"); |
| 50 | + db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)"); |
| 51 | + |
| 52 | + // first row |
| 53 | + int nb = db.exec("INSERT INTO test VALUES (NULL, \"test\")"); |
| 54 | + std::cout << "INSERT INTO test VALUES (NULL, \"test\")\", returned " << nb << std::endl; |
| 55 | + |
| 56 | + // second row |
| 57 | + nb = db.exec("INSERT INTO test VALUES (NULL, \"second\")"); |
| 58 | + std::cout << "INSERT INTO test VALUES (NULL, \"second\")\", returned " << nb << std::endl; |
| 59 | + |
| 60 | + // update the second row |
| 61 | + nb = db.exec("UPDATE test SET value=\"second-updated\" WHERE id='2'"); |
| 62 | + std::cout << "UPDATE test SET value=\"second-updated\" WHERE id='2', returned " << nb << std::endl; |
| 63 | + |
| 64 | + // Check the results : expect two row of result |
| 65 | + SQLite::Statement query(db, "SELECT * FROM test"); |
| 66 | + std::cout << "SELECT * FROM test :\n"; |
| 67 | + while (query.executeStep()) |
| 68 | + { |
| 69 | + std::cout << "row (" << query.getColumn(0) << ", \"" << query.getColumn(1) << "\")\n"; |
| 70 | + } |
| 71 | + |
| 72 | + db.exec("DROP TABLE test"); |
| 73 | + } |
| 74 | + catch (std::exception& e) |
| 75 | + { |
| 76 | + std::cout << "SQLite exception: " << e.what() << std::endl; |
| 77 | + return EXIT_FAILURE; // unexpected error : exit the example program |
| 78 | + } |
| 79 | + remove("test.db3"); |
| 80 | + |
| 81 | + std::cout << "everything ok, quitting\n"; |
| 82 | + |
| 83 | + return EXIT_SUCCESS; |
| 84 | +} |
0 commit comments