#includeint main() { // Create a new database file called "test.db" SQLite::Database db("test.db", SQLite::OPEN_CREATE|SQLite::OPEN_READWRITE); // Create a new table with a "name" column and an "age" column db.exec("CREATE TABLE people(name TEXT, age INTEGER)"); // Insert some data into the table SQLite::Statement stmt(db, "INSERT INTO people(name, age) VALUES (?, ?)"); stmt.bind(1, "John"); stmt.bind(2, 25); stmt.exec(); // Query the data and print it out SQLite::Statement query(db, "SELECT name, age FROM people"); while (query.executeStep()) { std::string name = query.getColumn(0); int age = query.getColumn(1); std::cout << name << " is " << age << " years old." << std::endl; } return 0; }
#includeint main() { // Connect to a MySQL database mysqlx::Session session("localhost", 33060, "user", "password", "testdb"); // Query data from a table and print out the results auto result = session.sql("SELECT * FROM people"); auto rows = result.execute(); for (auto row : rows) { std::string name = row[0]; int age = row[1]; std::cout << name << " is " << age << " years old." << std::endl; } return 0; }
#includeIn conclusion, a "cpp Db" library could be any C++ library that provides functionality for interacting with a database. The specific library and syntax will depend on the specific DBMS and library that is being used, but the general idea is to provide an interface or API for querying, inserting, updating, and deleting data from the database.#include #include #include #include // Define a class that maps to a "person" table in the database class person { public: person() {} person(const std::string& name, int age) : name_(name), age_(age) {} const std::string& name() const { return name_; } void name(const std::string& name) { name_ = name; } int age() const { return age_; } void age(int age) { age_ = age; } private: friend class odb::access; #pragma db id std::string name_; int age_; }; int main() { // Create a new SQLite database file called "test.db" odb::sqlite::database db("test.db"); // Insert a new person into the database odb::transaction t(db.begin()); t->persist(person("John", 25)); t.commit(); // Query the person from the database and print out the name and age odb::query q = db.query (); q.where("name = ?").bind("John"); person p = q.execute_one(); std::cout << p.name() << " is " << p.age() << " years old." << std::endl; return 0; }