首页主机资讯CentOS C++数据库连接怎样操作

CentOS C++数据库连接怎样操作

时间2025-12-17 23:00:04发布访客分类主机资讯浏览248
导读:在CentOS上使用C++连接数据库,通常需要使用数据库提供的客户端库和API。以下是一些常见数据库的连接操作步骤: MySQL 安装MySQL客户端库 sudo yum install mysql-devel 编写C++代码 使...

在CentOS上使用C++连接数据库,通常需要使用数据库提供的客户端库和API。以下是一些常见数据库的连接操作步骤:

MySQL

  1. 安装MySQL客户端库

    sudo yum install mysql-devel
    
  2. 编写C++代码 使用MySQL提供的C API来连接数据库。

    #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);
    
        }
    
    
        // 执行SQL查询
        if (mysql_query(conn, "SELECT * FROM table_name")) {
        
            fprintf(stderr, "%s\n", mysql_error(conn));
        
            exit(1);
    
        }
        
    
        res = mysql_use_result(conn);
    
    
        // 处理查询结果
        while ((row = mysql_fetch_row(res)) != NULL) {
        
            std::cout <
        <
         row[0] <
        <
         std::endl;
    
        }
        
    
        // 关闭连接
        mysql_free_result(res);
        
        mysql_close(conn);
        
    
        return 0;
    
    }
        
    
  3. 编译代码

    g++ -o mysql_example mysql_example.cpp -lmysqlclient
    
  4. 运行程序

    ./mysql_example
    

PostgreSQL

  1. 安装PostgreSQL客户端库

    sudo yum install postgresql-devel
    
  2. 编写C++代码 使用libpq库来连接数据库。

    #include <
        libpq-fe.h>
        
    #include <
        iostream>
    
    
    int main() {
        
        PGconn *conn;
        
        PGresult *res;
        
    
        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);
    
        }
        
    
        int rows = PQntuples(res);
        
        for (int i = 0;
         i <
         rows;
     i++) {
        
            std::cout <
        <
         PQgetvalue(res, i, 0) <
        <
         std::endl;
    
        }
        
    
        PQclear(res);
        
        PQfinish(conn);
        
    
        return 0;
    
    }
        
    
  3. 编译代码

    g++ -o postgres_example postgres_example.cpp -lpq
    
  4. 运行程序

    ./postgres_example
    

SQLite

  1. 安装SQLite开发库

    sudo yum install sqlite-devel
    
  2. 编写C++代码 使用SQLite的C API来连接数据库。

    #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("test.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;
    
    }
        
    
  3. 编译代码

    g++ -o sqlite_example sqlite_example.cpp -lsqlite3
    
  4. 运行程序

    ./sqlite_example
    

总结

以上步骤展示了如何在CentOS上使用C++连接不同类型的数据库。每种数据库都有其特定的客户端库和API,因此需要根据具体的数据库类型进行相应的配置和编码。编译时需要链接相应的库文件。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: CentOS C++数据库连接怎样操作
本文地址: https://pptw.com/jishu/774451.html
CentOS C++如何实现跨平台编译 CentOS C++多线程编程怎样入门

游客 回复需填写必要信息