采用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....");
}
相关推荐
mit6.82432 分钟前
[Meetily后端框架] Whisper转录服务器 | 后端服务管理脚本
c++·人工智能·后端·python
九皇叔叔2 小时前
【9】PostgreSQL 之 vacuum 死元组清理
数据库·postgresql
L_autinue_Star2 小时前
手写vector容器:C++模板实战指南(从0到1掌握泛型编程)
java·c语言·开发语言·c++·学习·stl
风雅的远行者2 小时前
mysql互为主从失效,重新同步
数据库·mysql
宇钶宇夕3 小时前
S7-1200 系列 PLC 中 SCL 语言的 PEEK 和 POKE 指令使用详解
运维·服务器·数据库·程序人生·自动化
绿蚁新亭3 小时前
Spring的事务控制——学习历程
数据库·学习·spring
scilwb4 小时前
占用栅格地图数据集
数据库
无小道4 小时前
c++--typedef和#define的用法及区别
c语言·开发语言·汇编·c++
mit6.8245 小时前
[Vroom] 位置与矩阵 | 路由集成 | 抽象,解耦与通信
c++·人工智能·算法
ChuHsiang5 小时前
【C++】模板(二)
c++