|
| 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-2019 Sebastien Rombauts (sebastien.rombauts@gmail.com) |
| 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 | +#include <string> |
| 17 | +#include <fstream> |
| 18 | + |
| 19 | +#include <SQLiteCpp/SQLiteCpp.h> |
| 20 | +#include <SQLiteCpp/VariadicBind.h> |
| 21 | + |
| 22 | + |
| 23 | +#ifdef SQLITECPP_ENABLE_ASSERT_HANDLER |
| 24 | +namespace SQLite |
| 25 | +{ |
| 26 | +/// definition of the assertion handler enabled when SQLITECPP_ENABLE_ASSERT_HANDLER is defined in the project (CMakeList.txt) |
| 27 | +void assertion_failed(const char* apFile, const long apLine, const char* apFunc, const char* apExpr, const char* apMsg) |
| 28 | +{ |
| 29 | + // Print a message to the standard error output stream, and abort the program. |
| 30 | + std::cerr << apFile << ":" << apLine << ":" << " error: assertion failed (" << apExpr << ") in " << apFunc << "() with message \"" << apMsg << "\"\n"; |
| 31 | + std::abort(); |
| 32 | +} |
| 33 | +} |
| 34 | +#endif |
| 35 | + |
| 36 | +/// Get example path |
| 37 | +static inline std::string getExamplePath() |
| 38 | +{ |
| 39 | + std::string filePath(__FILE__); |
| 40 | + return filePath.substr( 0, filePath.length() - std::string("main.cpp").length()); |
| 41 | +} |
| 42 | + |
| 43 | +/// Example Database |
| 44 | +static const std::string filename_example_db3 = getExamplePath() + "/example.db3"; |
| 45 | +/// Image |
| 46 | +static const std::string filename_logo_png = getExamplePath() + "/logo.png"; |
| 47 | + |
| 48 | + |
| 49 | +/// Object Oriented Basic example |
| 50 | +class Example |
| 51 | +{ |
| 52 | +public: |
| 53 | + // Constructor |
| 54 | + Example() : |
| 55 | + mDb(filename_example_db3), // Open a database file in readonly mode |
| 56 | + mQuery(mDb, "SELECT * FROM test WHERE weight > :min_weight")// Compile a SQL query, containing one parameter (index 1) |
| 57 | + { |
| 58 | + } |
| 59 | + virtual ~Example() |
| 60 | + { |
| 61 | + } |
| 62 | + |
| 63 | + /// List the rows where the "weight" column is greater than the provided aParamValue |
| 64 | + void ListGreaterThan (const int aParamValue) |
| 65 | + { |
| 66 | + std::cout << "ListGreaterThan (" << aParamValue << ")\n"; |
| 67 | + |
| 68 | + // Bind the integer value provided to the first parameter of the SQL query |
| 69 | + mQuery.bind(":min_weight", aParamValue); // same as mQuery.bind(1, aParamValue); |
| 70 | + |
| 71 | + // Loop to execute the query step by step, to get one a row of results at a time |
| 72 | + while (mQuery.executeStep()) |
| 73 | + { |
| 74 | + std::cout << "row (" << mQuery.getColumn(0) << ", \"" << mQuery.getColumn(1) << "\", " << mQuery.getColumn(2) << ")\n"; |
| 75 | + } |
| 76 | + |
| 77 | + // Reset the query to be able to use it again later |
| 78 | + mQuery.reset(); |
| 79 | + } |
| 80 | + |
| 81 | +private: |
| 82 | + SQLite::Database mDb; ///< Database connection |
| 83 | + SQLite::Statement mQuery; ///< Database prepared SQL query |
| 84 | +}; |
| 85 | + |
| 86 | +int main () |
| 87 | +{ |
| 88 | + // Using SQLITE_VERSION would require #include <sqlite3.h> which we want to avoid: use SQLite::VERSION if possible. |
| 89 | +// std::cout << "SQlite3 version " << SQLITE_VERSION << std::endl; |
| 90 | + std::cout << "SQlite3 version " << SQLite::VERSION << " (" << SQLite::getLibVersion() << ")" << std::endl; |
| 91 | + std::cout << "SQliteC++ version " << SQLITECPP_VERSION << std::endl; |
| 92 | + |
| 93 | + std::ifstream infile("/home/alan/Downloads/tmp/en-zh/UNv1.0.en-zh.en"); |
| 94 | + std::string line; |
| 95 | + //////////////////////////////////////////////////////////////////////////// |
| 96 | + // Simple batch queries example (5/7) : |
| 97 | + try |
| 98 | + { |
| 99 | + // Open a database file in create/write mode |
| 100 | + SQLite::Database db("test.db3", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE); |
| 101 | + std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n"; |
| 102 | + |
| 103 | + // Create a new table with an explicit "id" column aliasing the underlying rowid |
| 104 | + db.exec("DROP TABLE IF EXISTS en"); |
| 105 | + db.exec("CREATE TABLE en (sentence TEXT)"); |
| 106 | + SQLite::Statement query(db, "INSERT INTO en VALUES(?)"); |
| 107 | + |
| 108 | + while (std::getline(infile,line)) { |
| 109 | + SQLite::bind(query,line.c_str()); |
| 110 | + query.exec(); |
| 111 | + query.reset(); |
| 112 | + } |
| 113 | + } |
| 114 | + catch (std::exception& e) |
| 115 | + { |
| 116 | + std::cout << "SQLite exception: " << e.what() << std::endl; |
| 117 | + return EXIT_FAILURE; // unexpected error : exit the example program |
| 118 | + } |
| 119 | + |
| 120 | + std::cout << "everything ok, quitting\n"; |
| 121 | + |
| 122 | + return EXIT_SUCCESS; |
| 123 | +} |
0 commit comments