配置环境
下载源码包,并解压下载的压缩包
MySQL CAPI下载
在Visual Studio中新建一个项目
打开项目文件所在的目录,把解压好的文件夹放到含有.cpp的文件目录下
CAPI文件夹里面是这样的:
导入头文件 :解决方案->属性->C/C+±>常规
找到附件包含目录,将lib库的目录放入 .\CAPI\include
导入lib库:解决方案->属性->链接器->常规
找到附加库目录,添加./CAPI
,然后确定。
添加依赖项:解决方案->属性->链接器->输入
找到附加依赖项,添加:
- libmysql.lib
- libmysqld.lib
最后,需要添加动态链接库,将CAPI文件下的libmysql.dll
和libmysqld.dll
复制到编译好的 exe 程序文件目录下。
如果没有动态链接库运行会报错。
开发环境搭建好了,就可以进行程序的编写了。
程序编写
首先写一个连接mysql的客户端类
下面是MysqlClient.h中的声明
#ifndef _MysqlClient_H
#define _MysqlClient_H
#pragma once
#include <mysql.h>
//用二维数组来存数据库中表里面的内容
#include <vector>
#include <string>
//定义一个客户端类
class MysqlClient
{
//为了后续代码简洁,声明别名
typedef const char * c_str;
private:
MYSQL mysql_id;
public:
MysqlClient();
~MysqlClient();
/*连接数据库需要传入
username 用户名
pwd 密码
db 数据库
host ip地址
port 端口号
*/
//连接数据库
void connect(c_str username, c_str pwd, c_str db, c_str host,int port=3306);
//数据表用二维数组来接收
typedef std::vector<std::vector<std::string>> table_content;
//用于执行sql命令
table_content execute(std::string sql);
//退出连接
void exit();
//输出运算符重载
friend std::ostream& operator<<(std::ostream& os, MysqlClient::table_content t);
};
std::ostream& operator<<(std::ostream& os, MysqlClient::table_content t);
#endif
然后是MysqlClient.c中的类方法实现
#include "MysqlClient.h"
MysqlClient::MysqlClient()
{
//mysql的初始化
if (mysql_library_init(0, nullptr, nullptr) != 0)
{
throw "数据库初始化失败!";
}
//mysql结构体初始化
mysql_init(&mysql_id);
//设置初始化参数
mysql_options(&mysql_id, MYSQL_SET_CHARSET_NAME, "gbk");
}
//析构方法
MysqlClient::~MysqlClient()
{
//释放内存
mysql_library_end();
}
void MysqlClient::connect(c_str user, c_str pwd, c_str db, c_str host, int port)
{
if (!mysql_real_connect(&mysql_id, host, user, pwd, db, port, nullptr, 0))
{
throw "连接失败";
}
}
MysqlClient::table_content MysqlClient::execute(std::string sql)
{
//执行mysql命令
mysql_real_query(&mysql_id, sql.data(), sql.size());
//错误id,0代表正确执行
int erron_id = mysql_errno(&mysql_id);
//获取错误信息
const char* erron_msg = mysql_error(&mysql_id);
if (erron_id!=0)
{
printf("ERROR: %d : %s\n",erron, erron_msg);
return table_content();
}
//接收查询结果
MYSQL_RES *result = mysql_store_result(&mysql_id);
if (result==nullptr)
{
return table_content();
}
//获取表的列数
auto fields = mysql_num_fields(result);
MYSQL_ROW row;
table_content t_result;
while (row=mysql_fetch_row(result))
{
t_result.push_back(std::vector<std::string>());
for (unsigned int i = 0; i < fields;i++)
{
if (row[i] != nullptr)
{
t_result.back().push_back(row[i]);
}
}
}
return t_result;
}
void MysqlClient::exit()
{
mysql_close(&mysql_id);
}
std::ostream& operator<<(std::ostream& os, MysqlClient::table_content t)
{
for (auto& e2 : t)
{
for (auto& e : e2)
{
os << e << "\t";
}
os << std::endl;
}
return os;
}
接下来是主函数的测试
#include "MysqlClient.h"
#include<iostream>
using namespace std;
void test()
{
MysqlClient msc;
//连接root用户,密码为123456,数据库名为class_db,主机地址为本机
msc.connect("root", "123456", "class_db","127.0.0.1");
//存sql语句
string s;
while (true)
{
cout << "mysql>";
//获取手动输入的sql字符串
getline(cin, s);
if (s == "exit;")
{
break;
}
cout << "---------------------------------" << endl;
auto re = msc.execute(s);
cout << re;
cout << "---------------------------------" << endl;
}
msc.exit();
}
int main()
{
test();
//system("pause");
return 0;
}
运行程序,又回到了命令行写sql语句的时候了~
今天的分享就到这里了,希望大家能有所收获。
大家的支持就是我写文的动力~