Databases
C++ Database Connections
Connecting to Databases
C++ database connections use driver-specific APIs.
Understanding C++ Database Drivers
To connect a C++ application to a database, specific drivers are required. These drivers act as intermediaries between your application and the database, handling communication and transactions. Popular database systems like MySQL, PostgreSQL, and SQLite have their own C++ APIs.
Setting Up a MySQL Connection
To connect to a MySQL database, you must use the MySQL Connector/C++. This library provides the necessary API to connect and interact with MySQL databases using C++.
#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/statement.h>
#include <cppconn/resultset.h>
int main() {
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
driver = sql::mysql::get_mysql_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "user", "password");
con->setSchema("database_name");
stmt = con->createStatement();
res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
while (res->next()) {
std::cout << "Message: " << res->getString("_message") << std::endl;
}
delete res;
delete stmt;
delete con;
return 0;
}
Connecting to a PostgreSQL Database
For PostgreSQL, the libpq library is the standard way to connect to a PostgreSQL database from a C++ application. This library is robust and provides extensive functionality for database operations.
#include <iostream>
#include <libpq-fe.h>
int main() {
PGconn *conn = PQconnectdb("user=username password=secret dbname=mydb hostaddr=127.0.0.1 port=5432");
if (PQstatus(conn) != CONNECTION_OK) {
std::cerr << "Connection to database failed: " << PQerrorMessage(conn) << std::endl;
PQfinish(conn);
return 1;
}
PGresult *res = PQexec(conn, "SELECT version();");
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
std::cerr << "SELECT failed: " << PQerrorMessage(conn) << std::endl;
PQclear(res);
PQfinish(conn);
return 1;
}
std::cout << "PostgreSQL version: " << PQgetvalue(res, 0, 0) << std::endl;
PQclear(res);
PQfinish(conn);
return 0;
}
Working with SQLite in C++
SQLite is a serverless database engine that is self-contained and requires minimal setup. The SQLite C/C++ library is simple to integrate and use within C++ projects.
#include <iostream>
#include <sqlite3.h>
int main() {
sqlite3 *db;
char *errMsg = 0;
int rc = sqlite3_open("test.db", &db);
if (rc) {
std::cerr << "Can't open database: " << sqlite3_errmsg(db) << std::endl;
return 1;
} else {
std::cout << "Opened database successfully" << std::endl;
}
const char *sql = "CREATE TABLE IF NOT EXISTS COMPANY("
"ID INT PRIMARY KEY NOT NULL,"
"NAME TEXT NOT NULL,"
"AGE INT NOT NULL,"
"ADDRESS CHAR(50),"
"SALARY REAL );";
rc = sqlite3_exec(db, sql, 0, 0, &errMsg);
if (rc != SQLITE_OK) {
std::cerr << "SQL error: " << errMsg << std::endl;
sqlite3_free(errMsg);
} else {
std::cout << "Table created successfully" << std::endl;
}
sqlite3_close(db);
return 0;
}
Conclusion
Connecting C++ applications to databases involves using the right driver and API for your chosen database system. Each database has its own unique setup and API calls, but with the right configuration, C++ can efficiently handle database operations.
Databases
- SQLite
- PostgreSQL
- MongoDB
- Database Connections