Mysql函数接口
Nevermore 2023-01-11  DataBase
当使用yum安装默认会形成对应的Mysql库,ls /usr/lib64/mysql/,如没有,可到官网 (opens new window)下载对应的版本(编译需要指明 -I./include 和 -L./lib;运行需要export LD_LIBRARY_PATH=./lib)且(将lib路径复制,在/etc/ld.so.conf.d新建xxx.conf,将路径拷贝进去,再执行ldconfig)。
头文件的安装可以使用如下方法
wget https://downloads.mysql.com/archives/get/p/19/file/mysql-connector-c-devel-6.1.11-1.el7.x86_64.rpm
 1
sudo yum localinstall mysql-connector-c-devel-6.1.11-1.el7.x86_64.rpm 
 1
makefile的编写需要如下:
test:test.cc
	g++ -o $@ $^  -std=c++11 -I./incdlue -L/lib64/mysql -lmysqlclient -lpthread  -ldl -lrt
.PHONY:clean
clean:
	rm -rf test
 1
2
3
4
5
6
2
3
4
5
6
# C函数的使用
- 查看版本函数
 
mysql_get_client_info()
 1
- 使用函数访问数据库步骤
 
 1
MYSQL *STDCALL 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);
 1
2
3
4
5
6
7
2
3
4
5
6
7
- 示例代码
 
#include <iostream>
#include <cstdlib>
#include <mysql/mysql.h>
#include <string>
std::string host = "localhost";
std::string user = "root";
std::string password = "xxx";
std::string dp = "RemoteInnoDB";
const int port = 3306;
int main()
{
    std::cout << "client" << mysql_get_client_info() << std::endl;
    // 创建数据库句柄
    MYSQL *my = mysql_init(nullptr);
    // 连接数据库
    if(mysql_real_connect(my, host.c_str(), user.c_str(), password.c_str(), dp.c_str(), port, nullptr, 0) == nullptr) {
        std::cout << "connect failed" << std::endl;
        exit(1);
    }
    mysql_set_character_set(my, "utf8");
    std::cout << "success" << std::endl;
    //操作数据库
    // std::string inSql = "insert into test values(2, \'王五\')";
    std::string inSql = "select * from test";
    int ret = mysql_query(my, inSql.c_str());
    if(ret == 0) {
        std::cout << "insert success" << std::endl; 
    }else{
        std::cout << inSql << "-> failed" << std::endl; 
    }
    // 查看时对数据解析——针对select
    MYSQL_RES *result = mysql_store_result(my); // 该函数会malloc一块空间保存结果,要手动free(result)
    my_ulonglong row = mysql_num_rows(result);
    unsigned int col = mysql_num_fields(result);
    MYSQL_FIELD * colName = mysql_fetch_fields(result); //获取列明-表头
    for(int i = 0; i < col; i++) {
        std::cout << colName[i].name << "\t";
    }
    std::cout << std::endl;
    for(int i = 0; i < row; i++) {
        MYSQL_ROW oneLine = mysql_fetch_row(result); //返回二级指针,可看成字符串数组
        for(int j = 0; j < col; j++) {
            std::cout << oneLine[j] << "\t";
        }
        std::cout << std::endl;
    }
    free(result);
    mysql_close(my); // 关闭数据库
    return 0;
}
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
