安装环境:
系统 windows 7 IDE:vs2013
MySQL版本: 5.6.26
一、下载
本人使用的下载链接是 https://www.cr173.com/soft/105990.html
MySQL Connector/C++ 1.1.5 发布,此版本的二进制版本需要使用 Boost 1.54.0 编译。
Boost库下载地址:https://www.boost.org/users/history/version_1_54_0.html
二、安装
我的解压路径是:
Boost:E:\ThirdParty\Boost
MySQL:E:\ThirdParty\MySQLConnectorC++
CMD窗口下 切换到Boost安装目录
输入bootstrap.bat 回车
目录下多出这两个文件
然后在CMD中继续输入bjam.exe 开始构建Boost C++ Libraries
三、配置
本人所使用的编译器是VS2013
一、Boost的配置:
①新建一个空的控制台项目
②右键 进入 属性界面
选择左侧 C/C++ 附加包含目录输入Boost的安装目录
③属性 选择 链接器
包含 Boost中的libs目录
④测试是否安装成功
#include <boost/lexical_cast.hpp>
#include <iostream>
using namespace std;
int main()
{
using boost::lexical_cast;
int a = lexical_cast<int>("123");
double b = lexical_cast<double>("123.0123456789");
string s0 = lexical_cast<string>(a);
string s1 = lexical_cast<string>(b);
cout << "number: " << a << " " << b << endl;
cout << "string: " << s0 << " " << s1 << endl;
int c = 0;
try{
c = lexical_cast<int>("abcd");
}
catch (boost::bad_lexical_cast& e){
cout << e.what() << endl;
}
return 0;
}
输出:
至此,VS2013配置Boost结束
*二、MySQL Connector/C++ 配置
一、右键属性 C/C++
附加包含目录 添加 E:\ThirdParty\MySQLConnectorC++\include
二、属性 链接器 常规
包含 E:\ThirdParty\MySQLConnectorC++\lib\opt
三。链接器 输入选项
添加 mysqlcppconn.lib,mysqlcppconn-static.lib 两个静态库文件
四、添加dll文件
将MySQL Connector C++ 1.0.5\lib\opt下的mysqlcppconn.dll文件复制到项目所在路径下的debug文件夹以及项目所在文件夹
我的项目所在文件夹:
右键项目 --> 在文件资源管理器中打开文件夹 即可打开项目路径 如图所示,在文件夹中添加DLL 文件
五、测试
测试代码:
//#include <boost/lexical_cast.hpp>
//#include <iostream>
//using namespace std;
//int main()
//{
// using boost::lexical_cast;
// int a = lexical_cast<int>("123");
// double b = lexical_cast<double>("123.0123456789");
// string s0 = lexical_cast<string>(a);
// string s1 = lexical_cast<string>(b);
// cout << "number: " << a << " " << b << endl;
// cout << "string: " << s0 << " " << s1 << endl;
// int c = 0;
// try{
// c = lexical_cast<int>("abcd");
// }
// catch (boost::bad_lexical_cast& e){
// cout << e.what() << endl;
// }
// return 0;
//}
#include <iostream>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
#include <mysql_driver.h>
using namespace sql;
using namespace std;
class Mysql {
public:
mysql::MySQL_Driver *driver;
Connection *con;//连接指针
Statement *state;//状态集
ResultSet *result;//结果集
PreparedStatement *prep_stmt;
Mysql()//构造函数
{
driver = sql::mysql::get_mysql_driver_instance();
// 建立链接
con = driver->connect("tcp://127.0.0.1:3306", "root", "123456");
//连接地址 端口号 连接用户 密码
state = con->createStatement();
state->execute("use medicine");//使用哪个数据库(我的是 medicine )
cout << "=====" << endl;
}
void query()//查询
{
/*
使用数据库名为 medicine
表名为: user
含有字段: account password name classification
*/
// 查询
result = state->executeQuery("select * from user");//执行SQL语句
cout << result->rowsCount() << endl;//获取记录集数据
// 输出查询
while (result->next())
{
//cout << result->getString("account")<< endl;//之前直接使用getString() 导致 程序崩溃 后查阅资料 需要使用
//如下代码 方可正确 cout << result->getString("account").c_str() << endl;
cout << result->getString("account").c_str() << endl;//
}
}
~Mysql()//析构函数
{
delete state;
delete con;
}
};
int main()
{
Mysql m;
m.query();
return 0;
}
附录SQL语句代码: :
/*
Navicat MySQL Data Transfer
Source Server : MySql
Source Server Version : 50626
Source Host : localhost:3306
Source Database : medicine
Target Server Type : MYSQL
Target Server Version : 50626
File Encoding : 65001
Date: 2019-08-16 00:03:41
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`account` varchar(255) NOT NULL,
`password` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`classification` varchar(255) DEFAULT NULL,
PRIMARY KEY (`account`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('123456', '123456', '令狐冲', '系统');
INSERT INTO `user` VALUES ('beijing', 'bj', '北京', '管理员');
INSERT INTO `user` VALUES ('cat', 'cat', 'cat', '管理员');
INSERT INTO `user` VALUES ('catd', '123456', '随缘', '医生');
INSERT INTO `user` VALUES ('gz', 'gz', '广州', '管理员');
INSERT INTO `user` VALUES ('haha', 'yuri', 'Yuri', '售药员');
INSERT INTO `user` VALUES ('hehe', 'hehe', '云中鹤', '医生');
INSERT INTO `user` VALUES ('Kevin', '123456', 'Tom', '售药员');
INSERT INTO `user` VALUES ('kfc', 'kfc', 'kfc', '医生');
INSERT INTO `user` VALUES ('Lily', 'itcast', 'Lily', '售药员');
INSERT INTO `user` VALUES ('test', 'haha', 'Mike', '管理员');
INSERT INTO `user` VALUES ('yes', 'no', 'ok', '医生');
INSERT INTO `user` VALUES ('zmf', '123456', '随缘', '医生');
测试结果:
至此: 全部过程完毕