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服务器上,通过远程网络连接:

  1 #include <winsock.h>
  2 #include "include/mysql.h"
  3 #include <iostream>
  4 #include <string>
  5 #pragma comment(lib,"lib/libmysql.lib")
  6 #pragma comment(lib,"lib/mysqlclient.lib")
  7 using namespace std;
  8 
  9 int main()
 10 {
 11     cout << __FUNCTION__ << " is called." << endl;
 12     string sql;
 13     MYSQL mysql;
 14     try
 15     {    
 16         mysql_init(&mysql);    
 17         // 连接远程数据库
 18         if (NULL == mysql_real_connect(&mysql, "192.168.1.102", "root", "hongrui123", "mysql", 3306, NULL, 0))
 19         {    
 20             cout << __LINE__ << mysql_error(&mysql) << mysql_errno(&mysql) << endl;
 21             throw - 1;
 22         }
 23 
 24         //创建数据库hr_1
 25         sql = "create database if not exists hr_1;";
 26         if (mysql_query(&mysql, sql.c_str()))
 27         {
 28             cout << "line: " << __LINE__ << ";"<< mysql_error(&mysql) << mysql_errno(&mysql) << endl;
 29             throw - 1;
 30         }
 31         
 32         //进入数据库hr_1
 33         sql = "use hr_1;";
 34         if (mysql_query(&mysql, sql.c_str()))
 35         {
 36             cout << "line: " << __LINE__ << ";" << mysql_error(&mysql) << mysql_errno(&mysql) << endl;
 37             throw - 1;
 38         }
 39 
 40         //创建表hr_tbl
 41         sql = "create table if not exists `hr_tbl`(\
 42             `id` INT auto_increment,\
 43             `title` VARCHAR(100),\
 44             `name` VARCHAR(100),\
 45             primary key(id))\
 46             default charset = utf8;";
 47         if (mysql_query(&mysql, sql.c_str()))
 48         {
 49             cout << "line: " << __LINE__ << ";" << mysql_error(&mysql) << mysql_errno(&mysql) << endl;
 50             throw - 1;
 51         }
 52 
 53         //插入数据,事务
 54         sql = "begin;";
 55         mysql_query(&mysql, sql.c_str());
 56         sql = "insert into hr_tbl(title,name)\
 57                 values (\"hong\",\"rui\");";
 58         if (mysql_query(&mysql, sql.c_str()))
 59         {
 60             cout << "line: " << __LINE__ << ";" << mysql_error(&mysql) << mysql_errno(&mysql) << endl;
 61         }
 62         sql = "commit;";
 63         mysql_query(&mysql, sql.c_str());
 64         //更新数据
 65         sql = "update hr_tbl set title = 'huang' where id < 3;";
 66         if (mysql_query(&mysql, sql.c_str()))
 67         {
 68             cout << "line: " << __LINE__ << ";" << mysql_error(&mysql) << mysql_errno(&mysql) << endl;
 69         }
 70         
 71         //删除数据
 72         sql = "delete from hr_tbl where id > 4;";
 73         if (mysql_query(&mysql, sql.c_str()))
 74         {
 75             cout << "line: " << __LINE__ << ";" << mysql_error(&mysql) << mysql_errno(&mysql) << endl;
 76         }
 77 
 78         //查询数据
 79         sql = "select * from hr_tbl;";
 80         if (mysql_query(&mysql, sql.c_str()))
 81         {
 82             cout << "line: " << __LINE__ << ";" << mysql_error(&mysql) << mysql_errno(&mysql) << endl;
 83             throw -1;
 84         }
 85         else
 86         {
 87             //读取检索的结果
 88             MYSQL_RES *result = mysql_use_result(&mysql);
 89             if (result != NULL)
 90             {
 91                 MYSQL_ROW row;
 92                 int num_fields = mysql_num_fields(result);//每一行的字段数量
 93                 while (row = mysql_fetch_row(result))
 94                 {
 95                     if (row == NULL)
 96                     {
 97                         break;
 98                     }
 99                     else
100                     {
101                         for (int i= 0; i < num_fields; ++i)
102                         {
103                             cout << row[i]<<" ";
104                         }
105                         cout << endl;
106                     }
107                 }
108             }
109             mysql_free_result(result);
110         }
111     }
112     catch (...)
113     {
114         cout << "MySQL operation is error!" << endl;
115     }
116 
117     mysql_close(&mysql);
118     system("pause");
119     return 0;
120 }

 

posted @ 2020-01-29 21:59  ho966  阅读(1481)  评论(0编辑  收藏  举报