MySql:连接和关闭

c++ connector

下面是一个示例,世界使用c++ connector时注意release版本和debug版本要和响应的动态库匹配才可以

#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>

int main() {
    // 创建 MySQL Connector/C++ 驱动程序对象
    sql::mysql::MySQL_Driver *driver;
    sql::Connection *con;

    // 初始化驱动程序对象
    driver = sql::mysql::get_mysql_driver_instance();

    // 创建连接
    con = driver->connect("tcp://127.0.0.1:3306", "username", "password");

    // 连接到数据库
    con->setSchema("database_name");

    // 执行查询
    sql::Statement *stmt;
    sql::ResultSet *res;
    stmt = con->createStatement();
    res = stmt->executeQuery("SELECT * FROM your_table");

    // 处理结果集
    while (res->next()) {
        std::cout << "Column 1: " << res->getString(1) << std::endl;
        std::cout << "Column 2: " << res->getString(2) << std::endl;
    }

    // 释放资源
    delete res;
    delete stmt;
    delete con;

    return 0;
}

相关推荐

  1. MySql连接关闭

    2024-04-29 10:28:02       15 阅读
  2. Linux关闭SSH延迟连接超时自动注销

    2024-04-29 10:28:02       8 阅读
  3. MySQL 配置连接问题解决方案

    2024-04-29 10:28:02       40 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-29 10:28:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-29 10:28:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-29 10:28:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-29 10:28:02       20 阅读

热门阅读

  1. Python设计模式(二)

    2024-04-29 10:28:02       15 阅读
  2. 深入浅出MySQL-06-【索引的设计和使用】

    2024-04-29 10:28:02       12 阅读
  3. 代码随想录训练营28day-动态规划

    2024-04-29 10:28:02       12 阅读
  4. vite构建工具

    2024-04-29 10:28:02       11 阅读
  5. Ali-Sentinel-节点与度量

    2024-04-29 10:28:02       13 阅读
  6. db2 SQLCODE=-1585和SQLSTATE=54048

    2024-04-29 10:28:02       13 阅读
  7. Linux yum/环境变量/服务/进程/hash/bash/防火墙命令

    2024-04-29 10:28:02       17 阅读