转载自:https://www.cnblogs.com/ho966/p/12241634.html
注意:版权归原作者所有,转载仅供学习交流使用
C/C++操作MySQL API调用
记录下MySQL的C语言API的使用
1、Linux环境,操作系统是ubuntu
执行:sudo apt-get install libmysqlclient-dev
这样系统默认头文件路径(/usr/include)下就会多了一个mysql的文件夹,里面就是API头文件;
动态库路径/usr/lib/x86_64-linux-gnu/libmysqlclient.so。
2、Windows环境,操作系统win10
本地安装MySQL(官网有下载),vs新建工程,从安装目录(C:\Program Files (x86)\MySQL\MySQL Server 5.7)拷贝出include、lib两个文件夹到vs工程文件夹,
包含静态库通过方法:
#pragma comment(lib,“lib/libmysql.lib”)
#pragma comment(lib,“lib/mysqlclient.lib”)
包含动态库方法: 将libmysql.dll拷贝到生成exe的目录中
vs程序实例如下,mysql服务器是在linux服务器上,通过远程网络连接:
#include <winsock.h>
#include "include/mysql.h"
#include <iostream>
#include <string>
#pragma comment(lib,"lib/libmysql.lib")
#pragma comment(lib,"lib/mysqlclient.lib")
using namespace std;
int main()
{
cout << __FUNCTION__ << " is called." << endl;
string sql;
MYSQL mysql;
try
{
mysql_init(&mysql);
// 连接远程数据库
if (NULL == mysql_real_connect(&mysql, "192.168.1.102", "root", "hongrui123", "mysql", 3306, NULL, 0))
{
cout << __LINE__ << mysql_error(&mysql) << mysql_errno(&mysql) << endl;
throw - 1;
}
//创建数据库hr_1
sql = "create database if not exists hr_1;";
if (mysql_query(&mysql, sql.c_str()))
{
cout << "line: " << __LINE__ << ";"<< mysql_error(&mysql) << mysql_errno(&mysql) << endl;
throw - 1;
}
//进入数据库hr_1
sql = "use hr_1;";
if (mysql_query(&mysql, sql.c_str()))
{
cout << "line: " << __LINE__ << ";" << mysql_error(&mysql) << mysql_errno(&mysql) << endl;
throw - 1;
}
//创建表hr_tbl
sql = "create table if not exists `hr_tbl`(\
`id` INT auto_increment,\
`title` VARCHAR(100),\
`name` VARCHAR(100),\
primary key(id))\
default charset = utf8;";
if (mysql_query(&mysql, sql.c_str()))
{
cout << "line: " << __LINE__ << ";" << mysql_error(&mysql) << mysql_errno(&mysql) << endl;
throw - 1;
}
//插入数据,事务
sql = "begin;";
mysql_query(&mysql, sql.c_str());
sql = "insert into hr_tbl(title,name)\
values (\"hong\",\"rui\");";
if (mysql_query(&mysql, sql.c_str()))
{
cout << "line: " << __LINE__ << ";" << mysql_error(&mysql) << mysql_errno(&mysql) << endl;
}
sql = "commit;";
mysql_query(&mysql, sql.c_str());
//更新数据
sql = "update hr_tbl set title = 'huang' where id < 3;";
if (mysql_query(&mysql, sql.c_str()))
{
cout << "line: " << __LINE__ << ";" << mysql_error(&mysql) << mysql_errno(&mysql) << endl;
}
//删除数据
sql = "delete from hr_tbl where id > 4;";
if (mysql_query(&mysql, sql.c_str()))
{
cout << "line: " << __LINE__ << ";" << mysql_error(&mysql) << mysql_errno(&mysql) << endl;
}
//查询数据
sql = "select * from hr_tbl;";
if (mysql_query(&mysql, sql.c_str()))
{
cout << "line: " << __LINE__ << ";" << mysql_error(&mysql) << mysql_errno(&mysql) << endl;
throw -1;
}
else
{
//读取检索的结果
MYSQL_RES *result = mysql_use_result(&mysql);
if (result != NULL)
{
MYSQL_ROW row;
int num_fields = mysql_num_fields(result);//每一行的字段数量
while (row = mysql_fetch_row(result))
{
if (row == NULL)
{
break;
}
else
{
for (int i= 0; i < num_fields; ++i)
{
cout << row[i]<<" ";
}
cout << endl;
}
}
}
mysql_free_result(result);
}
}
catch (...)
{
cout << "MySQL operation is error!" << endl;
}
mysql_close(&mysql);
system("pause");
return 0;
}