遇坑: 项目使用debug模式, mysql-connector-cpp 下载的是release ,始终连接不上mysql,bad allocation。
使用mysql jdbc类型的API连接mysql, 项目使用Debug 必须下载debug版本的api, 不然无法连接
main.cpp
#include <iostream>
#include "mysql/jdbc.h"
int main(int argc, char** argv)
{
sql::mysql::MySQL_Driver *driver;
sql::Connection *conn;
driver = sql::mysql::get_mysql_driver_instance();
try{
conn = driver->connect("127.0.0.1:3306", "root", "root");
std::cout<<conn->getClientInfo();
conn->setSchema("test");
std::unique_ptr<sql::Statement> stmt(conn->createStatement());
stmt->execute("create table hello(id int)");
conn->close();
delete conn;
} catch (std::exception &e) {
std::cout<<e.what();
}
return 0;
}
CMakeFiles.txt
cmake_minimum_required(VERSION 3.27)
project(mysqltest)
set(CMAKE_CXX_STANDARD 14)
include_directories(
D:/library/vc/mysql-connector-c++-8.3.0-winx64/include
)
add_executable(mysqltest main.cpp)
target_link_directories(mysqltest PRIVATE
D:/library/vc/mysql-connector-c++-8.3.0-winx64/lib64/vs14
)
target_link_libraries(mysqltest PRIVATE mysqlcppconn)
版权声明:本文为NII.CN的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://nii.cn/4272.html 发布者:nii