/*** @file main.cpp* @brief A few short examples in a row.** Demonstrates how-to use the SQLite++ wrapper** Copyright (c) 2012-2023 Sebastien Rombauts (sebastien.rombauts@gmail.com)** Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt* or copy at http://opensource.org/licenses/MIT)*/#include <iostream>#include <cstdio>#include <cstdlib>#include <SQLiteCpp/SQLiteCpp.h>#ifdef SQLITECPP_ENABLE_ASSERT_HANDLERnamespace SQLite{/// definition of the assertion handler enabled when SQLITECPP_ENABLE_ASSERT_HANDLER is defined in the project (CMakeList.txt)void assertion_failed(const char* apFile, const long apLine, const char* apFunc, const char* apExpr, const char* apMsg){// Print a message to the standard error output stream, and abort the program.std::cerr << apFile << ":" << apLine << ":" << " error: assertion failed (" << apExpr << ") in " << apFunc << "() with message \"" << apMsg << "\"\n";std::abort();}}#endifint main (){// Using SQLITE_VERSION would require #include <sqlite3.h> which we want to avoid: use SQLite::VERSION if possible.// std::cout << "SQlite3 version " << SQLITE_VERSION << std::endl;std::cout << "SQlite3 version " << SQLite::VERSION << " (" << SQLite::getLibVersion() << ")" << std::endl;std::cout << "SQliteC++ version " << SQLITECPP_VERSION << std::endl;////////////////////////////////////////////////////////////////////////////// Simple batch queries example :try{// Open a database file in create/write modeSQLite::Database db("test.db3", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE);std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n";// Create a new table with an explicit "id" column aliasing the underlying rowiddb.exec("DROP TABLE IF EXISTS test");db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)");// first rowint nb = db.exec("INSERT INTO test VALUES (NULL, \"test\")");std::cout << "INSERT INTO test VALUES (NULL, \"test\")\", returned " << nb << std::endl;// second rownb = db.exec("INSERT INTO test VALUES (NULL, \"second\")");std::cout << "INSERT INTO test VALUES (NULL, \"second\")\", returned " << nb << std::endl;// update the second rownb = db.exec("UPDATE test SET value=\"second-updated\" WHERE id='2'");std::cout << "UPDATE test SET value=\"second-updated\" WHERE id='2', returned " << nb << std::endl;// Check the results : expect two row of resultSQLite::Statement query(db, "SELECT * FROM test");std::cout << "SELECT * FROM test :\n";while (query.executeStep()){std::cout << "row (" << query.getColumn(0) << ", \"" << query.getColumn(1) << "\")\n";}db.exec("DROP TABLE test");}catch (std::exception& e){std::cout << "SQLite exception: " << e.what() << std::endl;return EXIT_FAILURE; // unexpected error : exit the example program}remove("test.db3");std::cout << "everything ok, quitting\n";return EXIT_SUCCESS;}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。