采用connector-c++ 8.0操作数据库

1.下载最新的Connector

https://dev.mysql.com/downloads/connector/cpp/,下载带debug的库。

解压缩到本地,本次使用的是带debug模式的connector库:

注:其中mysqlcppconn与mysqlcppconn8的区别是:

2.在cmakelist中定义寻址头文件的路径与库的路径

复制代码
#定义头文件需要寻址的路径
include_directories(
    D:/Library/mysql-connector-c++-8.2.0-winx64/include/jdbc
)

#定义库文件需要寻址的路径
link_directories(
    D:/Library/mysql-connector-c++-8.2.0-winx64/lib64/vs14/debug
)

target_link_libraries(QtDemo2
        Qt5::Core
        Qt5::Gui
        Qt5::Widgets
        ......
        mysqlcppconn
)

3.复制对应的DLL到可执行目录

把D:\Library\mysql-connector-c+±8.2.0-winx64\lib64\debug中的mysqlcppconn-9-vs14.dll复制到项目的可执行目录下。

3.基础使用

https://dev.mysql.com/doc/connector-cpp/1.1/en/connector-cpp-examples-connecting.html

cpp 复制代码
#include "sql_connector.h"
#include "string"
#include <QDebug>

#include "mysql_connection.h"
#include "cppconn/driver.h"
#include "cppconn/exception.h"
#include "cppconn/prepared_statement.h"



void SqlConnector::process()
{
        qDebug("SqlConnector started....");


        std::string server = "tcp://localhost:3306";
        std::string username = "root";
        std::string password = "Xi1ozh#1";

        sql::Driver* driver;
        sql::Connection* connection;

        try
        {
                driver = get_driver_instance();
                connection = driver->connect(server, username, password);
                connection->setSchema("ws300");
        }
        catch (sql::SQLException e)
        {
                std::cout << "Could not connect to server. Error message: " << e.what() << "\n";
        }

        //查询
        sql::SQLString sql("select * from users;");
        std::unique_ptr<sql::Statement> statment(connection->createStatement());
        statment->execute("set names utf8mb4");
        std::unique_ptr<sql::ResultSet> result(statment->executeQuery(sql));
        while (result->next())
        {
                auto data = result->getString("password").asStdString();
                qDebug("SqlConnector username: %s", data.c_str());
        }

        result->close();
        statment->close();
        connection->close();
        delete connection;
        qDebug("SqlConnector finished....");
}
相关推荐
tudousisi222几秒前
8.25第二题训练
c++
星星.7225 分钟前
2026河南萌新联赛第六场(郑州大学)补题B、D、L、J、I
数据结构·c++·算法
心平气和量大福大10 分钟前
android-实例2-数据库sqlite(查询)
android·数据库·sqlite
海兰12 分钟前
【数据库】告别数据库性能噩梦,拥抱 UUID v7
数据库
linweidong17 分钟前
字节AI Agent面试题大全及参考答案(下)
数据库·oracle
枫彩24 分钟前
WorkBuddy 做A股自动复盘实战:数据校验、工具调用与定时报告
大数据·网络·数据库·a股·mcp·workbuddy
witton28 分钟前
xmake中将proto文件生成C/C++文件然后再编译链接进项目
c语言·c++·mingw·protobuf·xmake·protobuf-c·protoc-gen-c
牛油果子哥q31 分钟前
C++异常处理万字详解:try-throw-catch机制、栈展开、标准异常体系、自定义异常、工程规范、内存泄漏避坑
开发语言·jvm·c++
其实防守也摸鱼33 分钟前
CTF 入门到进阶:Web、逆向、密码学与 Pwn 实战解题指南
数据库·安全·自动化·github·copilot
晓子文集34 分钟前
Tushare接口文档:每日停复牌信息(suspend_d)
大数据·数据库·金融·金融数据·量化投资