采用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....");
}
相关推荐
saltymilk13 小时前
使用 C++ 模拟 ShaderLanguage 的 swizzle
c++·模板元编程
DolphinDB13 小时前
集成 Prometheus 与 DolphinDB 规则引擎,构建敏捷监控解决方案
数据库
IvorySQL14 小时前
PostgreSQL 技术日报 (3月10日)|IIoT 性能瓶颈与内核优化新讨论
数据库·postgresql·开源
DBA小马哥17 小时前
时序数据库是什么?能源行业国产化替换的入门必看
数据库·时序数据库
爱可生开源社区19 小时前
某马来西亚游戏公司如何从 SQL Server 迁移至 OceanBase?
数据库
xlp666hub19 小时前
Leetcode第五题:用C++解决盛最多水的容器问题
linux·c++·leetcode
得物技术21 小时前
搜索 C++ 引擎回归能力建设:从自测到工程化准出|得物技术
c++·后端·测试
小瓦码J码21 小时前
PostgreSQL表名超长踩坑记
数据库·postgresql
yhyyht21 小时前
InfluxDB入门记录(三)flux-dsl
数据库·后端
IvorySQL2 天前
PostgreSQL 技术日报 (3月9日)|EXPLAIN ANALYZE 计时优化与复制语法讨论
数据库·postgresql·开源