设为首页 加入收藏

TOP

C++操作MYSQL遇到的一些问题
2018-11-04 18:11:30 】 浏览:103
Tags:操作 MYSQL 遇到 一些 问题

首先 我使用的是 vcpkg<不知道的可以进行百度 可以剧透一下,这个对Visual Studio使用一些C++的轮子太方便了,

上面是我装的一些库《大大安利vcpkg 一定要使用poweshell进行编译 

装上vcpkg后装ligmariadb这个库,这个对Mysql操作还是挺方便的,然后说一下 在装库时碰到的一些问题 

1.网络问题

1.参考了一下 github上的答案 将下载失败的url拷下来 自己下 然后再重新开始安装《vcpkg在安装时每一个需要下载的 都会提供url-仔细找,

2.这个问题 也可以隔一短时间 再下载 亲测没有毛病

二. 在使用mysql.h遇到的一些问题

1.Can't initialize character set unknown (path: compiled_in)

这个问题,我查了很多地方,才找到原因,是我当时安装mysql时没有使用cmake将其编译成全部字集使用。这个问题也很好解决,就是使用mysql_option()这个函数,

    mysql_options(con,
        MYSQL_SET_CHARSET_NAME,
        MYSQL_AUTODETECT_CHARSET_NAME);
//成功返回零,失败返回非零

将其编码格式定为操作系统自动提供的样式。

二. Plugin caching_sha2_password could not be loaded: 找不到指定的模块。

MySQL provides two authentication plugins that implement SHA-256 hashing for user account passwords《这是官方给的 也只有8以及以上的版本才会遇到这个问题

官方提供了一种插件 来增强 MySQL密码的可靠性 我找了一下相应的api或其他mysql_option()

还真让我找到了一个

MYSQL_OPT_GET_SERVER_PUBLIC_KEY (argument type: bool *)

Enables the client to request from the server the public key required for RSA key pair-based password exchange. 
This option applies to clients that authenticate with the caching_sha2_password authentication plugin.
For that plugin, the server does not send the public key unless requested.
This option is ignored for accounts that do not authenticate with that plugin.
It is also ignored if RSA-based password exchange is not used, as is the case when the client connects to the server using a secure connection.

但是好像c api好像还不能用 亲测不能用,所以我将我的一个用户的默认密码验证从这个插件改回了mysql_native老方式

亲测成功

在这贴上代码 很简单的 《我的问题 有可能不是你们的问题,但还是可以用作参考

 1 #include<errno.h>
 2 #include<stdio.h>
 3 #include<string>
 4 #include<iostream>
 5 #include<mysql/mysql.h>
 6 #define host "localhost"
 7 #define user "root"
 8 #define pass "123"
 9 #define db "msdb"
10 static inline void _mysql_check(MYSQL* con) {
11     fprintf(stderr, "%s", mysql_error(con));
12 //    std::cerr << mysql_error(con) << std::endl;
13     //mysql_close(con);
14     exit(EXIT_FAILURE);
15 }
16 
17 int main()
18 {
19     MYSQL* con=mysql_init(0);
20     MYSQL_RES* result=NULL;
21     MYSQL_FIELD *fd;
22     MYSQL_ROW sql_rom;
23     std::cout << con << std::endl;
24     mysql_options(con,
25         MYSQL_SET_CHARSET_NAME,
26         MYSQL_AUTODETECT_CHARSET_NAME);
27     /*if (!mysql_real_connect(con, host, user, pass, db, 3306, NULL, 0)) {
28         _mysql_check(con);
29     }
30     */
31     con = mysql_real_connect(con, host, user, pass, db, 3306, NULL, 0);
32     if (con) {
33         if (!mysql_select_db(con, db)) {
34             printf("connect successfule \n");
35             mysql_options(con, MYSQL_SET_CHARSET_NAME, "gbk");
36             if (!mysql_query(con, "SELECT * FROM student"))
37             {
38                 result = mysql_store_result(con);
39                 int j = mysql_num_fields(result);
40                 for (int i = 0; fd = mysql_fetch_field(result); i++) {
41                     printf("%s\t", fd->name);
42                 }
43                 printf("\n");
44                 while (sql_rom = mysql_fetch_row(result)) {
45                     for (int i = 0; i < j; i++) {
46                         printf("%s\t", sql_rom[i]);
47                     }
48                     printf("\n");
49                 }
50             }
51         }
52 
53     }
54     mysql_free_result(result);
55     mysql_close(con);
56     getchar();
57     return 0;
58 }

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C++ Primer 3rd Edition 中文版 下一篇洛谷11月月赛题解(A-C)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目