#include#include #include #include using namespace std; int main() { // establish connection to database sql::Driver* driver = get_driver_instance(); sql::Connection* con = driver->connect("tcp://127.0.0.1:3306", "username", "password"); con->setSchema("database_name"); // execute query and print results sql::Statement* stmt = con->createStatement(); sql::ResultSet* res = stmt->executeQuery("SELECT * FROM table_name"); while (res->next()) { cout << "Column 1: " << res->getString("column1") << endl; cout << "Column 2: " << res->getString("column2") << endl; } // clean up delete res; delete stmt; delete con; return 0; }
#includeIn both of these examples, the code is using a specific package library (MySQL Connector/C++ or SQLiteCpp) to connect to and execute queries in a database (MySQL or SQLite). These libraries provide C++ classes and functions that make it easier to work with SQL databases from within C++ code.#include #include using namespace std; int main() { // establish connection to database SQLite::Database db("my_database.sqlite3"); // execute query and print results SQLite::Statement query(db, "SELECT * FROM my_table"); while (query.executeStep()) { string column1 = query.getColumn(0).getText(); int column2 = query.getColumn(1).getInt(); cout << "Column 1: " << column1 << endl; cout << "Column 2: " << column2 << endl; } return 0; }