From 416958f094db23bf60e575cf9766963dd7f869ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Rombauts?= Date: Thu, 30 Apr 2015 22:08:26 +0200 Subject: [PATCH] Add sample code for getColumn() by name - sample code provided by #46 - rework example for better readability --- examples/example1/main.cpp | 87 +++++++++++++++++++++++++++++--------- 1 file changed, 67 insertions(+), 20 deletions(-) diff --git a/examples/example1/main.cpp b/examples/example1/main.cpp index e94603a..92cb7d2 100644 --- a/examples/example1/main.cpp +++ b/examples/example1/main.cpp @@ -83,7 +83,8 @@ private: int main () { - // Basic example (1/6) : + //////////////////////////////////////////////////////////////////////////// + // Very basic first example (1/7) : try { // Open a database file in readonly mode @@ -95,8 +96,24 @@ int main () std::cout << "SQLite table 'test' exists=" << bExists << "\n"; // Get a single value result with an easy to use shortcut - std::string value = db.execAndGet("SELECT value FROM test WHERE id=2"); + const std::string value = db.execAndGet("SELECT value FROM test WHERE id=2"); std::cout << "execAndGet=" << value.c_str() << std::endl; + } + catch (std::exception& e) + { + std::cout << "SQLite exception: " << e.what() << std::endl; + return EXIT_FAILURE; // unexpected error : exit the example program + } + + //////////////////////////////////////////////////////////////////////////// + // Simple select query - few variations (2/7) : + try + { + // Open a database file in readonly mode + SQLite::Database db(filename_example_db3); // SQLITE_OPEN_READONLY + std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n"; + + ///// a) Loop to get values of column by index, using auto cast to variable type // Compile a SQL query, containing one parameter (index 1) SQLite::Statement query(db, "SELECT id as test_id, value as test_val, weight as test_weight FROM test WHERE weight > ?"); @@ -105,6 +122,24 @@ int main () query.bind(1, 2); std::cout << "binded with integer value '2' :\n"; + // Loop to execute the query step by step, to get one a row of results at a time + while (query.executeStep()) + { + // Demonstrates how to get some typed column value (and the equivalent explicit call) + const int id = query.getColumn(0); // = query.getColumn(0).getInt(); + //const char* pvalue = query.getColumn(1); // = query.getColumn(1).getText(); + const std::string value = query.getColumn(1); // = query.getColumn(1).getText(); + const int bytes = query.getColumn(1).size(); // .getColumn(1).getBytes(); + const double weight = query.getColumn(2); // = query.getColumn(2).getInt(); + std::cout << "row (" << id << ", \"" << value.c_str() << "\"(" << bytes << ") " << weight << ")\n"; + } + + ///// b) Get aliased column names (and original column names if possible) + + // Reset the query to use it again + query.reset(); + std::cout << "SQLite statement '" << query.getQuery().c_str() << "' reseted (" << query.getColumnCount() << " columns in the result)\n"; + // Execute the first step of the query, to get the fist row of results, and name of columns if (query.executeStep()) { @@ -113,6 +148,7 @@ int main () const std::string name1 = query.getColumn(1).getName(); const std::string name2 = query.getColumn(2).getName(); std::cout << "aliased result [\"" << name0.c_str() << "\", \"" << name1.c_str() << "\", \"" << name2.c_str() << "\"]\n"; + #ifdef SQLITE_ENABLE_COLUMN_METADATA // Show how to get origin names of the table columns from which theses result columns come from. // Requires the SQLITE_ENABLE_COLUMN_METADATA preprocessor macro to be @@ -122,23 +158,34 @@ int main () const std::string oname2 = query.getColumn(2).getOriginName(); std::cout << "origin table 'test' [\"" << oname0.c_str() << "\", \"" << oname1.c_str() << "\", \"" << oname2.c_str() << "\"]\n"; #endif - // Demonstrates how to get some typed column value (and the equivalent explicit call) - const int id = query.getColumn(0); // = query.getColumn(0).getInt(); - //const char* pvalue = query.getColumn(1); // = query.getColumn(1).getText(); - const std::string value2 = query.getColumn(1); // = query.getColumn(1).getText(); - const int bytes = query.getColumn(1).getBytes(); - const double weight = query.getColumn(2); // = query.getColumn(2).getInt(); - - std::cout << "row (" << id << ", \"" << value2.c_str() << "\" " << bytes << " bytes, " << weight << ")\n"; + // Demonstrates that inserting column value in a std:ostream is natural + std::cout << "row (" << query.getColumn(0) << ", \"" << query.getColumn(1) << "\", " << query.getColumn(2) << ")\n"; } - - // Loop to execute the query step by step, to get one a row of results at a time + // Loop to execute the rest of the query step by step, to get one a row of results at a time while (query.executeStep()) { // Demonstrates that inserting column value in a std:ostream is natural std::cout << "row (" << query.getColumn(0) << ", \"" << query.getColumn(1) << "\", " << query.getColumn(2) << ")\n"; } + ///// c) Get columns by name + + // Reset the query to use it again + query.reset(); + std::cout << "SQLite statement '" << query.getQuery().c_str() << "' reseted (" << query.getColumnCount() << " columns in the result)\n"; + + // Loop to execute the query step by step, to get one a row of results at a time + while (query.executeStep()) + { + // Demonstrates how to get column value by aliased name (not the original table names, see above) + const int id = query.getColumn("test_id"); + const std::string value = query.getColumn("test_val"); + const double weight = query.getColumn("test_weight"); + std::cout << "row (" << id << ", \"" << value.c_str() << "\" " << weight << ")\n"; + } + + ///// d) Uses explicit typed getters instead of auto cast operators + // Reset the query to use it again query.reset(); std::cout << "SQLite statement '" << query.getQuery().c_str() << "' reseted (" << query.getColumnCount () << " columns in the result)\n"; @@ -147,14 +194,14 @@ int main () std::cout << "binded with string value \"6\" :\n"; // Reuses variables: uses assignement operator in the loop instead of constructor with initialization int id = 0; - std::string value2; + std::string value; double weight = 0.0; while (query.executeStep()) { id = query.getColumn(0).getInt(); - value2 = query.getColumn(1).getText(); + value = query.getColumn(1).getText(); weight = query.getColumn(2).getInt(); - std::cout << "row (" << id << ", \"" << value2 << "\", " << weight << ")\n"; + std::cout << "row (" << id << ", \"" << value << "\", " << weight << ")\n"; } } catch (std::exception& e) @@ -164,7 +211,7 @@ int main () } //////////////////////////////////////////////////////////////////////////// - // Object Oriented Basic example (2/6) : + // Object Oriented Basic example (3/7) : try { // Open the database and compile the query @@ -181,7 +228,7 @@ int main () return EXIT_FAILURE; // unexpected error : exit the example program } - // The execAndGet wrapper example (3/6) : + // The execAndGet wrapper example (4/7) : try { // Open a database file in readonly mode @@ -201,7 +248,7 @@ int main () } //////////////////////////////////////////////////////////////////////////// - // Simple batch queries example (4/6) : + // Simple batch queries example (5/7) : try { // Open a database file in create/write mode @@ -242,7 +289,7 @@ int main () remove("test.db3"); //////////////////////////////////////////////////////////////////////////// - // RAII transaction example (5/6) : + // RAII transaction example (6/7) : try { // Open a database file in create/write mode @@ -310,7 +357,7 @@ int main () remove("transaction.db3"); //////////////////////////////////////////////////////////////////////////// - // Binary blob and in-memory database example (6/6) : + // Binary blob and in-memory database example (7/7) : try { // Open a database file in create/write mode