使用第三方库MySQL Connector/C++是啊比
系统是win10,编译器是visual studio2022
测试代码如下
#include <jdbc/cppconn/statement.h>
#include <jdbc/cppconn/resultset.h>
#include <jdbc/cppconn/exception.h>
#include "jdbc/mysql_driver.h"
#include<windows.h>
int main() {
std::string sql1 = "create database testmysql";//sql1用于创建testmysql数据库
SetConsoleOutputCP(CP_UTF8);
try
{
// 注册MySQL驱动程序
sql::mysql::MySQL_Driver* driver = sql::mysql::get_mysql_driver_instance();
sql::Connection* con;
sql::Statement* stmt;
sql::ResultSet* res;
//获取数据库连接对象
con = driver->connect("tcp://localhost:3306", "root", "123456");
//获取执行语句对象
stmt = con->createStatement();
stmt->execute(sql1);
delete stmt;
delete con;
}
catch (sql::SQLException& sqle)
{
std::cout << "数据库连接出错" << std::endl;
}
return 0;
}
设置属性
vc目录下的库目录为:C:\mysql-connector-c++-8.4.0-winx64-debug\mysql-connector-c++-8.4.0-winx64\lib64\vs14;C:\mysql-connector-c++-8.4.0-winx64-debug\mysql-connector-c++-8.4.0-winx64\lib64\vs14\debug;$(LibraryPath)
C/C++的库目录为C:\mysql-connector-c++-8.4.0-winx64-debug\mysql-connector-c++-8.4.0-winx64\include
链接器附加依赖项为
mysqlcppconn.lib
mysqlcppconn8.lib
但是运行后报错
不知道,哪里错了,有什么遗漏。