1. 程式人生 > >C++操作MYSQL遇到的一些問題

C++操作MYSQL遇到的一些問題

自動 方便 int str rom local loaded adb 百度

首先 我使用的是 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++操作MYSQL遇到的一些問題