本文實(shí)例為大家分享了VS2017調(diào)用MySQL 8.0的具體方法,供大家參考,具體內(nèi)容如下
簡(jiǎn)述
在網(wǎng)上找了一些解答,發(fā)現(xiàn)都有些小問(wèn)題。
就自己寫(xiě)一個(gè)吧
配置
配置很關(guān)鍵。
我的MySQL安裝目錄為 C:\Program Files\MySQL
2的部分寫(xiě)的地址: C:\Program Files\MySQL\MySQL Server 8.0\include
3的部分寫(xiě)的地址: C:\Program Files\MySQL\MySQL Server 8.0\lib
配置鏈接器:
2中寫(xiě)的內(nèi)容為: libmysql.lib
最后一步: 移動(dòng)這個(gè)libmysql.dll 到C:\Windows\System32 目錄下
代碼
注意!!上面的這一步非常重要!!
- pwd:為密碼
- root:賬號(hào)
- 3306:登陸端口號(hào)
- jxgl:是我數(shù)據(jù)庫(kù)中的一個(gè)database
- 執(zhí)行的命令中的student是我這個(gè)表
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
|
#include <stdio.h> #include <mysql.h> // 如果配置ok就可以直接包含這個(gè)文件 int main( void ) { MYSQL mysql; //一個(gè)數(shù)據(jù)庫(kù)結(jié)構(gòu)體 MYSQL_RES* res; //一個(gè)結(jié)果集結(jié)構(gòu)體 MYSQL_ROW row; //char** 二維數(shù)組,存放一條條記錄 //初始化數(shù)據(jù)庫(kù) mysql_init(&mysql); //設(shè)置編碼方式 mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "gbk" ); //連接數(shù)據(jù)庫(kù) //判斷如果連接失敗就輸出連接失敗。 if (mysql_real_connect(&mysql, "localhost" , "root" , "pwd" , "jxgl" , 3306, NULL, 0) == NULL) printf ( "連接失敗!\\n" ); //查詢(xún)數(shù)據(jù) mysql_query(&mysql, "select * from student" ); //獲取結(jié)果集 res = mysql_store_result(&mysql); //給ROW賦值,判斷ROW是否為空,不為空就打印數(shù)據(jù)。 while (row = mysql_fetch_row(res)) { printf ( "%s " , row[0]); //打印ID printf ( "%s " , row[1]); //打印姓名 printf ( "%s " , row[2]); printf ( "%s \n" , row[3]); } //釋放結(jié)果集 mysql_free_result(res); //關(guān)閉數(shù)據(jù)庫(kù) mysql_close(&mysql); //停留等待 system ( "pause" ); return 0; } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/a19990412/article/details/83617923