溫馨提示×

Ubuntu C++如何連接數據庫

小樊
74
2025-02-20 10:37:12
欄目: 編程語言

在Ubuntu上使用C++連接數據庫,通常有幾種常見的數據庫系統可以選擇,如MySQL、PostgreSQL、SQLite等。以下是使用C++連接這些數據庫的基本步驟和示例代碼。

1. MySQL

安裝MySQL客戶端庫

首先,你需要安裝MySQL客戶端庫:

sudo apt update
sudo apt install libmysqlclient-dev

示例代碼

#include <mysql/mysql.h>
#include <iostream>

int main() {
    MYSQL *conn;
    MYSQL_RES *res;
    MYSQL_ROW row;

    // 初始化連接
    conn = mysql_init(NULL);

    // 連接到數據庫
    if (!mysql_real_connect(conn, "localhost", "user", "password", "database", 0, NULL, 0)) {
        fprintf(stderr, "%s\n", mysql_error(conn));
        exit(1);
    }

    // 執行查詢
    if (mysql_query(conn, "SELECT * FROM table_name")) {
        fprintf(stderr, "%s\n", mysql_error(conn));
        exit(1);
    }

    // 獲取結果集
    res = mysql_store_result(conn);

    // 遍歷結果集
    while ((row = mysql_fetch_row(res))) {
        std::cout << row[0] << " " << row[1] << std::endl;
    }

    // 釋放資源
    mysql_free_result(res);
    mysql_close(conn);

    return 0;
}

2. PostgreSQL

安裝PostgreSQL客戶端庫

首先,你需要安裝PostgreSQL客戶端庫:

sudo apt update
sudo apt install libpq-dev

示例代碼

#include <libpq-fe.h>
#include <iostream>

int main() {
    PGconn *conn;
    PGresult *res;
    int rows, i;

    // 連接到數據庫
    conn = PQconnectdb("host=localhost dbname=database user=user password=password");

    if (PQstatus(conn) != CONNECTION_OK) {
        std::cerr << "Connection to database failed: " << PQerrorMessage(conn) << std::endl;
        PQfinish(conn);
        exit(1);
    }

    // 執行查詢
    res = PQexec(conn, "SELECT * FROM table_name");

    if (PQresultStatus(res) != PGRES_TUPLES_OK) {
        std::cerr << "SELECT failed: " << PQerrorMessage(conn) << std::endl;
        PQclear(res);
        PQfinish(conn);
        exit(1);
    }

    // 獲取行數
    rows = PQntuples(res);

    // 遍歷結果集
    for (i = 0; i < rows; i++) {
        std::cout << PQgetvalue(res, i, 0) << " " << PQgetvalue(res, i, 1) << std::endl;
    }

    // 釋放資源
    PQclear(res);
    PQfinish(conn);

    return 0;
}

3. SQLite

安裝SQLite3

首先,你需要安裝SQLite3:

sudo apt update
sudo apt install libsqlite3-dev

示例代碼

#include <sqlite3.h>
#include <iostream>

static int callback(void *data, int argc, char **argv, char **azColName) {
    for (int i = 0; i < argc; i++) {
        std::cout << azColName[i] << ": " << (argv[i] ? argv[i] : "NULL") << std::endl;
    }
    std::cout << std::endl;
    return 0;
}

int main() {
    sqlite3 *db;
    char *err_msg = 0;
    int rc;

    // 打開數據庫
    rc = sqlite3_open("database.db", &db);

    if (rc != SQLITE_OK) {
        std::cerr << "Cannot open database: " << sqlite3_errmsg(db) << std::endl;
        sqlite3_close(db);
        return 1;
    }

    // 執行查詢
    rc = sqlite3_exec(db, "SELECT * FROM table_name", callback, 0, &err_msg);

    if (rc != SQLITE_OK) {
        std::cerr << "SQL error: " << err_msg << std::endl;
        sqlite3_free(err_msg);
        sqlite3_close(db);
        return 1;
    }

    // 關閉數據庫
    sqlite3_close(db);

    return 0;
}

編譯和運行

對于上述每個示例,你可以使用以下命令進行編譯和運行:

MySQL

g++ -o mysql_example mysql_example.cpp -lmysqlclient
./mysql_example

PostgreSQL

g++ -o postgresql_example postgresql_example.cpp -lpq
./postgresql_example

SQLite

g++ -o sqlite_example sqlite_example.cpp -lsqlite3
./sqlite_example

請根據你的具體需求和數據庫類型選擇合適的示例代碼,并確保替換示例中的數據庫連接信息(如主機名、用戶名、密碼、數據庫名和表名)。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女