VC++2010配置使用MySQL5.6
0、前提
安装后的文件概览
编译器: VC++2010
MySQL版本:MySQL5.6.19 for win64
Connector版本:connector c++ 1.1.3
在VS2010下配置使用MySQL
1、配置头文件
项目属性--VC++目录--包含目录
2、配置库文件
在connector c++ 1.1.3\lib目录下有两个目录:debug目录 和 opt目录
lib\debug目录
lib\opt目录
由于有debug目录,所以猜测opt目录可能是类似release目录的优化(optimize)后的文件,因此在VC++中使用时在Debug下使用debug目录下的库文件,在Release模式下使用opt目录下的库目录。
eg.
#ifdef _DEBUG
#pragma comment(lib, "debug下的mysqlcppconn.lib")
#pragma comment(lib, "debug下的mysqlcppconn-static.lib")
#else
#pragma comment(lib, "opt下的mysqlcppconn.lib")
#pragma comment(lib, "opt下的mysqlcppconn-static.lib")
#endif
另外,在Debug或Release模式下将debug或opt目录下的mysqlcppcon.dll拷贝到项目目录下或system32目录下。 将 MySQL\MySQL Server5.6\lib目录下的libmysql.dll拷贝到项目目录下或system32目录下。
3、配置项目
由于该版本的MySQL是64位的,因此使用该MySQL的connector的项目必须被配置为X64类型的。 否则会有链接错误! 这一点要注意!
4、Demo
数据库:db_1220, 表:tbl_user, MySQL服务器:本地的localhost
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#pragma warning(disable:4251)
#ifdef _DEBUG
#pragma comment(lib, "D:\\Program Files\\MySQL\\Connector C++ 1.1.3\\lib\\debug\\mysqlcppconn-static.lib")
#pragma comment(lib, "D:\\Program Files\\MySQL\\Connector C++ 1.1.3\\lib\\debug\\mysqlcppconn.lib")
#else
#pragma comment(lib, "D:\\Program Files\\MySQL\\Connector C++ 1.1.3\\lib\\opt\\mysqlcppconn-static.lib")
#pragma comment(lib, "D:\\Program Files\\MySQL\\Connector C++ 1.1.3\\lib\\opt\\mysqlcppconn.lib")
#endif
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
sql::Driver *driver = NULL;
sql::Connection *con = NULL;
sql::Statement *stmt = NULL;
sql::ResultSet *res = NULL;
sql::SQLString strHost("localhost");
sql::SQLString strUser("root");
sql::SQLString strPwd("XXXXXXX");
sql::SQLString strSchema("db_1220");
sql::SQLString strQuery("select * from tbl_user");
try
{
driver = get_driver_instance();
con = driver->connect(strHost, strUser, strPwd);
con->setSchema(strSchema);
stmt = con->createStatement();
res = stmt->executeQuery(strQuery);
sql::ResultSetMetaData* pMetaData = res->getMetaData();
cout << endl;
cout << "Results have " << res->rowsCount() << " rows" << endl << endl;
while(res->next())
{
//get data by column name
cout << res->getInt("id")
<< " "
<< res->getString("name").c_str() //sql::SQLString没有重载<<操作符,因此不能直接cout<<res->getString("name")
<< " "
<< res->getString("password").c_str()
<< endl;
//get data by column index
cout << res->getInt(1)
<< " "
<< res->getString(2).c_str()
<< " "
<< res->getString(3).c_str()
<< endl;
}
}
catch (sql::SQLException& e)
{
cerr << endl << e.what() << endl;
}
catch (...)
{
cerr << endl << "some exception happeded" << endl;
}
if (NULL != res)
delete res;
if (NULL != stmt)
delete stmt;
if (NULL != con)
delete con;
cout << endl << endl;
return 0;
}
运行结果:
5、补充
如果在编译过程中报错找不到类似 “<boost/variant.hpp>”这样的错误信息,则是需要boost库支持,下载boost库配置一下即可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗