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

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

时间2025-12-20 00:37:03发布访客分类主机资讯浏览220
导读:在CentOS上使用C++连接数据库,通常需要使用数据库提供的客户端库和开发头文件。以下是一些常见数据库的连接操作步骤: MySQL 安装MySQL客户端库和开发头文件: sudo yum install mysql-devel...

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

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);
    
        }
    
    
        if (mysql_query(conn, "SHOW TABLES")) {
        
            fprintf(stderr, "%s\n", mysql_error(conn));
        
            exit(1);
    
        }
        
    
        res = mysql_store_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++代码: 使用libpqxx库来连接数据库。

    #include <
        pqxx/pqxx>
        
    #include <
        iostream>
    
    
    int main() {
    
        try {
        
            pqxx::connection C("dbname=database user=user password=password hostaddr=127.0.0.1 port=5432");
    
            if (C.is_open()) {
        
                std::cout <
        <
         "Opened database successfully: " <
        <
         C.dbname() <
        <
         std::endl;
        
                pqxx::work W(C);
        
    
                pqxx::result R = W.exec("SELECT * FROM table_name");
    
    
                for (auto row : R) {
        
                    std::cout <
        <
         row[0].as<
        std::string>
        () <
        <
         std::endl;
    
                }
        
    
                W.commit();
    
            }
     else {
        
                std::cout <
        <
         "Can't open database" <
        <
         std::endl;
    
            }
    
        }
         catch (const std::exception &
    e) {
        
            std::cerr <
        <
         e.what() <
        <
         std::endl;
        
            return 1;
    
        }
        
    
        return 0;
    
    }
        
    
  3. 编译代码

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

    ./postgresql_example
    

SQLite

  1. 安装SQLite3客户端库和开发头文件

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

    #include <
        sqlite3.h>
        
    #include <
        iostream>
    
    
    static int callback(void *NotUsed, 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/776861.html
CentOS如何配置C++网络库 CentOS Sniffer能检测到哪些网络攻击

游客 回复需填写必要信息