目录
1.下载Mysql的C接口库
2.程序中引入Mysql头文件和库文件
3.Mysql接口介绍
(1)压缩包解压之后最重要的两个文件夹
(2) 如何使mysql头文件和库文件引入到项目中?
(3)代码测试
#include
#include"./include/mysql.h"
int main()
{
//显示MySQL客户端库版本
std::cout << "mysql client version: " << mysql_get_client_info() << std::endl;
return 0;
}
mysql_ops:mysql_ops.cc
g++ -o $@ $^ -std=c++11 -I./include -L./lib -lmysqlclient
.PHONY:clean
clean:
rm -f mysql_ops
③解决OS找不到动态库的方法 - 动静态库
#include
#include
#include "./include/mysql.h"
const std::string host = "49.232.80.153";
const std::string user = "gsx";
const std::string passwd = "";
const std::string db = "mysql_test";
const int port = 3306;
int main()
{
// 相当于给我们创建了一个mysql句柄
MYSQL *my = mysql_init(nullptr);
// 链接数据库
if( nullptr == mysql_real_connect(my, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0))
{
std::cerr << "mysql 连接失败!" << std::endl;
return 1;
}
// 设置连接的编码也是utf8
mysql_set_character_set(my, "utf8");
std::cout << "mysql 连接成功!" << std::endl;
//增删改
std::string sql = "insert into user values (4,21, \'王五\')";
mysql_query(my, sql.c_str());
// std::string sql = "update user set age=18 where id=1";
// std::string sql = "delete from user where id=4";
// 查找所有的数据
// 1. 执行查询语句
std::string select_sql = "select * from user";
if (0 != mysql_query(my, select_sql.c_str()))
{
std::cout << "execute: " << select_sql << " failed!" << std::endl;
return 2;
}
std::cout << "execute: [" << select_sql << " ] success" << std::endl;
// 2. 获取查询结果数据
MYSQL_RES *res = mysql_store_result(my);
// 3. 数据的行列信息
// 获取结果行数
int rows = mysql_num_rows(res);
// 获取列数
int cols = mysql_num_fields(res);
// 获取所有的列属性
MYSQL_FIELD *fields = mysql_fetch_field(res);
for (int i = 0; i < cols; i++)
{
// 获取列名,通常没什么用
std::cout << fields[i].name << "\t";
}
std::cout << std::endl;
for(int i = 0; i < rows; i++)
{
// 获取一行具体的数据
MYSQL_ROW row = mysql_fetch_row(res);
for(int j = 0; j < cols; j++)
{
std::cout << row[j] << "\t";
}
std::cout << std::endl;
}
free(res); //不要忘记释放空间
mysql_close(my);
std::cout << "关闭mysql连接" << std::endl;
return 0;
}
(1) 连接数据库
MYSQL *mysql_real_connect(
MYSQL *mysql,
const char *host,
const char *user,
const char *passwd,
const char *db, //要访问哪个数据库
unsigned int port,
const char *unix_socket,
unsigned long clientflag
);
(2) 连接示例演示
#include
#include
#include "./include/mysql.h"
const std::string host = "49.232.80.153";
const std::string user = "gsx";
const std::string passwd = "";
const std::string db = "mysql_test";
const int port = 3306;
int main()
{
// 相当于给我们创建了一个mysql句柄
MYSQL* my = mysql_init(nullptr);
// 链接数据库
if( nullptr == mysql_real_connect(my, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0))
{
std::cerr << "mysql 连接失败!" << std::endl;
return 1;
}
std::cout << "mysql 连接成功!" << std::endl;
mysql_close(my);
std::cout << "关闭mysql连接" << std::endl;
(3)下发mysql命令mysql_query
int mysql_query(MYSQL *mysql, const char *q);
①增
std::string sql = "insert into user values (4,21, \'wangwu\')";
mysql_query(my, sql.c_str());
②改
std::string sql = "update user set age=18 where id=1";
mysql_query(my, sql.c_str());
③删
std::string sql = "delete from user where id=4";
mysql_query(my, sql.c_str());
(4)查询结果
MYSQL_RES *mysql_store_result(MYSQL *mysql);
my_ulonglong mysql_num_rows(MYSQL_RES *res);
unsigned int mysql_num_fields(MYSQL_RES *res);
MYSQL_FIELD *mysql_fetch_fields(MYSQL_RES *res);
// 查找所有的数据
// 1. 执行查询语句
std::string select_sql = "select * from user";
if (0 != mysql_query(my, select_sql.c_str()))
{
std::cout << "execute: " << select_sql << " failed!" << std::endl;
return 2;
}
std::cout << "execute: [" << select_sql << " ] success" << std::endl;
// 2. 获取查询结果数据
MYSQL_RES *res = mysql_store_result(my);
// 3. 数据的行列信息
// 获取结果行数
int rows = mysql_num_rows(res);
// 获取列数
int cols = mysql_num_fields(res);
// 获取所有的列属性
MYSQL_FIELD *fields = mysql_fetch_field(res);
for (int i = 0; i < cols; i++)
{
// 获取列名,通常没什么用
std::cout << fields[i].name << "\t";
}
std::cout << std::endl;
for(int i = 0; i < rows; i++)
{
// 获取一行具体的数据
MYSQL_ROW row = mysql_fetch_row(res);
for(int j = 0; j < cols; j++)
{
std::cout << row[j] << "\t";
}
std::cout << std::endl;
}
free(res); //不要忘记释放空间
// 设置连接的编码也是utf8
mysql_set_character_set(my, "utf8");
(5) mysql C api还支持事务等常用操作
my_bool STDCALL mysql_autocommit(MYSQL * mysql, my_bool auto_mode);
my_bool STDCALL mysql_commit(MYSQL * mysql);
my_bool STDCALL mysql_rollback(MYSQL * mysql);