采用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....");
}
相关推荐
ajassi200018 分钟前
开源 C++ QT Widget 开发(十五)多媒体--音频播放
linux·c++·qt·开源
睡觉的时候不会困1 小时前
Redis 主从复制详解:原理、配置与主从切换实战
数据库·redis·bootstrap
程序员的世界你不懂3 小时前
【Flask】测试平台开发,新增说明书编写和展示功能 第二十三篇
java·前端·数据库
鹅毛在路上了3 小时前
C++, ffmpeg, libavcodec-RTSP拉流,opencv实时预览
c++·opencv·ffmpeg
自学也学好编程3 小时前
【数据库】Redis详解:内存数据库与缓存之王
数据库·redis
John_ToDebug3 小时前
定制 ResourceBundle 的实现与 DuiLib 思想在 Chromium 架构下的应用解析
c++·chrome·ui
JAVA不会写3 小时前
在Mybatis plus中如何使用自定义Sql
数据库·sql
IT 小阿姨(数据库)3 小时前
PgSQL监控死元组和自动清理状态的SQL语句执行报错ERROR: division by zero原因分析和解决方法
linux·运维·数据库·sql·postgresql·centos
ChinaRainbowSea4 小时前
7. LangChain4j + 记忆缓存详细说明
java·数据库·redis·后端·缓存·langchain·ai编程
小欣加油4 小时前
leetcode 面试题01.02判定是否互为字符重排
数据结构·c++·算法·leetcode·职场和发展