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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM