?導(dǎo)航小助手?
? ? ???寫在前面
? ? ? ? ? ??一、準(zhǔn)備工作
? ? ? ? ? ? ? ?????1.1 把 libmysql.dll 和 libmysql.lib 文件復(fù)制到工程目錄下
? ? ? ? ? ? ? ?????1.2 添加 libmysql.lib?
? ? ? ? ? ? ? ?????1.3 添加 include目錄
? ? ? ? ? ? ? ?????1.4 包含頭文件
? ? ? ? ? ? ? ?????1.5 其他準(zhǔn)備工作
? ? ? ? ? ? ? ? ? ? ? ? ? ??????1.5.1 代碼準(zhǔn)備
? ? ? ? ? ? ? ? ? ? ? ? ? ??????1.5.2 創(chuàng)建數(shù)據(jù)庫連接
? ? ? ? ? ? ? ?????1.6 選擇相應(yīng)方案配置 Release,解決方案平臺(tái) x64
? ? ? ? ? ??二、代碼測(cè)試案例
? ? ? ? ? ? ? ?????2.1 向數(shù)據(jù)庫插入數(shù)據(jù)
? ? ? ? ? ? ? ?????2.2 向數(shù)據(jù)庫刪除數(shù)據(jù)
? ? ? ? ? ? ? ?????2.3 修改數(shù)據(jù)
? ? ? ? ? ? ? ?????2.4 查詢數(shù)據(jù),打印出來
? ? ? ? ? ??三、使用C語言連接MySQL數(shù)據(jù)庫 的測(cè)試案例代碼
寫在前面
有鐵鐵私信說 知道了 Java中使用 JDBC編程 來連接數(shù)據(jù)庫了,但是使用 C語言 來連接數(shù)據(jù)庫卻總是連接不上去~
立即安排一波使用 C語言連接 MySQL數(shù)據(jù)庫的教程~
一、準(zhǔn)備工作
1.1 把 libmysql.dll 和 libmysql.lib 文件復(fù)制到工程目錄下
首先,我們要找到剛剛開始下載的 MySQL數(shù)據(jù)庫 的安裝目錄,打開目錄,并且將 libmysql.dll文件 和 libmysql.lib文件 復(fù)制到工程目錄下~
我安裝MySQL的路徑:C:\Program Files\MySQL\MySQL Server 5.7\lib
1.2 添加 libmysql.lib?
?
1.3 添加 include目錄
我的這個(gè)路徑是:C:\Program Files\MySQL\MySQL Server 5.7\include?
來到 VS2019 頁面,和上述操作一樣~
右鍵MySQL工程,選擇屬性 右鍵,選擇 C/C++,選擇常規(guī),選擇 附加包含目錄,同時(shí)進(jìn)行編輯,將剛剛復(fù)制的 include路徑 給粘貼上去~
??
?
1.4 包含頭文件
接下來 在VS2019里面,先后敲出代碼:
#include <WinSock.h> 和 #include <mysql.h> ,需要注意的是,兩行代碼的順序不可以改變~?
?
1.5 其他準(zhǔn)備工作
1.5.1 代碼準(zhǔn)備
#include <iostream>
using namespace std;
#include <WinSock.h>
#include <mysql.h>
void test();
int main()
{
cout << "main" << endl;
test();
getchar();
return 0;
}
void test()
{
printf("test\n");
MYSQL m; //mysql連接
MYSQL_RES* res; //查詢結(jié)果集
MYSQL_ROW row; //二維數(shù)組,存放數(shù)據(jù)
//初始化數(shù)據(jù)庫
mysql_init(&m);
//設(shè)置編碼方式
mysql_options(&m, MYSQL_SET_CHARSET_NAME, "gbk");
//連接數(shù)據(jù)庫
if (mysql_real_connect(&m, "localhost", "root", "111111", "c", 3306, NULL, 0))
{ //主機(jī) 用戶名 密碼 數(shù)據(jù)庫名 端口
printf("數(shù)據(jù)庫連接成功\n");
}
else {
printf("數(shù)據(jù)庫連接失敗:%s \n", mysql_error(&m));
//輸出錯(cuò)誤信息
}
}
1.5.2 創(chuàng)建數(shù)據(jù)庫連接
第一步,Win+R,輸入 cmd~
?第二步,輸入 musql -uroot -p,回車后,輸入一開始安裝MySQL數(shù)據(jù)庫是設(shè)置的密碼,之后回車進(jìn)入數(shù)據(jù)庫~
第三步,開始創(chuàng)建一個(gè)新的數(shù)據(jù)庫,并且開始建表之類的操作~
1.6 選擇相應(yīng)方案配置 Release,解決方案平臺(tái) x64
最終,來運(yùn)行一下代碼,發(fā)現(xiàn)已經(jīng)成功了~
?
二、代碼測(cè)試案例
在經(jīng)過上述的一系列操作之后,已經(jīng)可以連接上 MySQL數(shù)據(jù)庫中了~
接下來我們就可以來做一些測(cè)試案例~
2.1 向數(shù)據(jù)庫插入數(shù)據(jù)
此時(shí),再插入數(shù)據(jù)之前,我們可以來看一看 上面新建的 student表 是沒有任何數(shù)據(jù)的~
但是,在加入 插入數(shù)據(jù)的代碼之后,運(yùn)行一下,我們就會(huì)發(fā)現(xiàn) 多出來一條數(shù)據(jù)~
?當(dāng)然,改成插入多條數(shù)據(jù)也是可以的啦~
2.2 向數(shù)據(jù)庫刪除數(shù)據(jù)
?
2.3 修改數(shù)據(jù)
沒修改之前:
修改之后:
2.4 查詢數(shù)據(jù),打印出來
三、使用C語言連接MySQL數(shù)據(jù)庫 的測(cè)試案例代碼
/*
//向數(shù)據(jù)庫插入數(shù)據(jù)
const char* sql = "insert into student values(2,'李四','女'),(3,'王五','男'),"
"(4, '趙六', '女')";
if (mysql_query(&m, sql))
{
printf("插入數(shù)據(jù)失?。?s \n", mysql_error(&m));
}
else
{
printf("插入數(shù)據(jù)成功\n");
}
//向數(shù)據(jù)庫刪除數(shù)據(jù)
const char* sql_2 = "delete from student where name = '趙六'";
if (mysql_query(&m, sql_2))
{
printf("刪除數(shù)據(jù)失?。?s \n", mysql_error(&m));
}
else
{
printf("刪除數(shù)據(jù)成功\n");
}
//向數(shù)據(jù)庫修改數(shù)據(jù)
const char* sql_3 = "update student set id = 5 where name = '李四'";
if (mysql_query(&m, sql_3))
{
printf("修改數(shù)據(jù)失?。?s \n", mysql_error(&m));
}
else
{
printf("修改數(shù)據(jù)成功\n");
}
*/
//需要注意的是,在下面的設(shè)置中,所查詢到的數(shù)據(jù) 與打印出來的數(shù)據(jù)要相互匹配,
//不然就會(huì)出現(xiàn)亂碼的
//查詢數(shù)據(jù)
const char* sql_4 = "select id,sex,name from student where name = '張三'";
if (mysql_query(&m, sql_4))
{
printf("未查到記錄:%s \n", mysql_error(&m));
}
else
{
printf("查詢成功 \n");
}
//獲取查詢結(jié)果集
res = mysql_store_result(&m);
if (res)
{
printf("獲取到數(shù)據(jù)\n");
}
else
{
printf("未獲取到數(shù)據(jù):%s \n", mysql_error(&m));
}
//打印獲取到的數(shù)據(jù)
printf("id\tname\tsex\n");
while (row = mysql_fetch_row(res))
{
printf("%s\t%s\t%s\n", row[0], row[1], row[2]);
}
//釋放資源
mysql_free_result(res);//釋放結(jié)果集
mysql_close(&m);//關(guān)閉數(shù)據(jù)庫
}
使用 C語言 連接 MySQL數(shù)據(jù)庫,正好可以和 Java 連接 MySQL數(shù)據(jù)庫 相互映襯~
好了,這篇博客就暫時(shí)介紹到這里了~
如果感覺這一篇博客對(duì)你有幫助的話,可以一鍵三連走一波,非常非常感謝啦 ~文章來源:http://www.zghlxwxcb.cn/news/detail-443792.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-443792.html
到了這里,關(guān)于【MySQL系列】使用C語言來連接數(shù)據(jù)庫的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!