日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql C语言API接口及实例

發布時間:2025/5/22 数据库 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql C语言API接口及实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Mysql數據庫動態庫:?libmysql.dll??libmysql.lib?mysql.h??WinSock2.h

Mysql?API數據結構???(mysql.h)

???????MYSQL:連接數據庫前,必須先創建MYSQL變量,此變量在很多Mysql?API函數會用到。它包含了一些連接信息等數據。

???????MYSQL_RES:MYSQL_RES結構體中包含了查詢結果集,也就是從數據庫中查詢到的數據。可以使用mysql_store_resultmysql_use_result函數獲得。

???????MYSQL_ROW:MYSQL?ROW的定義如下:typedef?char?**MYSQL_ROW;可見,它實際上是char?**類型,指向一個字符串數組。存儲一行中各段字符數組,可以通過mysql_fetch_row函數獲得。

???????MYSQL_FIELD:MYSQL_FIELD中包含了字段名、字段類型和大小等信息。可以重復調用mysql_fetch_field函數獲得所有字段的信息。

?

?????Mysql?C?API編程步驟

1、首先我們要包含mysql的頭文件,并鏈接mysql動態庫。

#include?<WinSock2.h>??????//?進行網絡編程需要winsock2.h???

#include?<mysql.h>???

#pragma?comment(lib,?"libmysql.lib")??

?2、創建MYSQL變量。如:

???????MYSQL?mysql;

3、初始化MYSQL變量。

???????mysql_init(&mysql);

?4、調用mysql_real_connect函數連接Mysql數據庫。

mysql_real_connect函數的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);????

???????參數說明:mysql--前面定義的MYSQL變量;host--MYSQL服務器的地址;user--登錄用戶名;passwd--登錄密碼;db--要連接的數據庫;port--MYSQL服務器的TCP服務端口;unix_socket--unix連接方式,為NULL時表示不使用socket或管道機制;clientflag--Mysql運行為ODBC數據庫的標記,一般取0

???????連接失敗時該函數返回0

?5、調用mysql_real_query函數進行數據庫查詢。mysql_real_query函數的原型如下:

???????int??STDCALL?mysql_real_query(MYSQL?*mysql,?const?char?*q,?unsigned?long?length);

???????參數說明:mysql--前面定義的MYSQL變量;q--SQL查詢語句;length--查詢語句的長度。

???????查詢成功則該函數返回0

???????6、通過調用mysql_store_resultmysql_use_result函數返回的MYSQL_RES變量獲取查詢結果數據。

???????兩個函數的原型分別為:

???????MYSQL_RES?*?????STDCALL?mysql_store_result(MYSQL?*mysql);

???????MYSQL_RES?*?????STDCALL?mysql_use_result(MYSQL?*mysql);

???????這兩個函數分別代表了獲取查詢結果的兩種方式。第一種,調用mysql_store_result函數將從Mysql服務器查詢的所有數據都存儲到客戶端,然后讀取;第二種,調用mysql_use_result初始化檢索,以便于后面一行一行的讀取結果集,而它本身并沒有從服務器讀取任何數據,這種方式較之第一種速度更快且所需內存更少,但它會綁定服務器,阻止其他線程更新任何表,而且必須重復執行mysql_fetch_row讀取數據,直至返回NULL,否則未讀取的行會在下一次查詢時作為結果的一部分返回,故經常我們使用mysql_store_result

???????7、調用mysql_fetch_row函數讀取結果集數據。

???????上述兩種方式最后都是重復調用mysql_fetch_row函數讀取數據。mysql_fetch_row函數的原型如下:

???????MYSQL_ROW?STDCALL?mysql_fetch_row(MYSQL_RES?*result);

???????參數result就是mysql_store_resultmysql_use_result的返回值。

???????該函數返回MYSQL_ROW型的變量,即字符串數組,假設為row,則row[i]為第i個字段的值。當到結果集尾部時,此函數返回NULL

???????8、結果集用完后,調用mysql_free_result函數釋放結果集,以防內存泄露。mysql_free_result函數的原型如下:

???????void??STDCALL?mysql_free_result(MYSQL_RES?*result);

???????9、不再查詢Mysql數據庫時,調用mysql_close函數關閉數據庫連接。mysql_close函數的原型為:

???????void?STDCALL?mysql_close(MYSQL?*sock);

?

例子:

1.?int?main()??{?

2.?????MYSQL?mysql;??

3.?????MYSQL_RES?*res;??

4.?????MYSQL_ROW?row;??

5.?????mysql_init(&mysql);??//?初始化MYSQL變量??

6.?????//?連接Mysql服務器,本例使用本機作為服務器。訪問的數據庫名稱為"msyql",參數中的user為你的登錄用戶名,***為登錄密碼,需要根據你的實際用戶進行設置??

7.?????if?(!mysql_real_connect(&mysql,?"127.0.0.1",?"user",?"123",?"mysql",?3306,?0,?0))??{??

8.?????????cout?<<?"mysql_real_connect?failure!"?<<?endl;??

9.?????????return?0;??

10.?????}??

11.?????if?(mysql_real_query(&mysql,?"select?*?from?user",?(unsigned?long)strlen("select?*?from?user"))){????//?查詢mysql數據庫中的user?

12.?????????cout?<<?"mysql_real_query?failure!"?<<?endl;??

13.?????????return?0;??

14.?????}??//?存儲結果集??

15.?????res?=?mysql_store_result(&mysql);??

16.?????if?(NULL?==?res)?{??

17.?????????cout?<<?"mysql_store_result?failure!"?<<?endl;??

18.?????????return?0;??

19.?????}??

20.?????//?重復讀取行,并輸出第一個字段的值,直到rowNULL??

21.?????while?(row?=?mysql_fetch_row(res))??{??

22.?????????cout?<<?row[0]?<<?endl;??

23.?????}??

24.?????mysql_free_result(res);??//?釋放結果集???

25.?????mysql_close(&mysql);??//?關閉Mysql連接

26.???????return?0;??}??

?

10.Char?*?mysql_get_client_info()?顯示mysql客戶端版本

??MySQL?client?version:?5.0.38

11.int?mysql_num_fields(MYSQL_RES?*result)?返回結果子表中域(字段)的個數

12.?MYSQL_FIELD?*?mysql_fetch_field(MYSQL_RES?*result)?返回結果子表中的域結構體指針

13.Void?mysql_real_escape_string(MYSQL*?con,?char*?savedata,?char?*data,?int?size)?在將二進制數據(非文本)保存到數據庫之前,需要轉義,否則數據庫不能正常保存,取出數據時,無需解轉移。轉義時一個字符轉義后2個字符,所以savedata內存必須為data2

14.?unsigned?long?*?mysql_fetch_lengths(MYSQL_RES?*result)?獲取結果中各個字符串的長度,返回為1維數組

?

一些有用的例子:

#include?

#include?

?

int?main(int?argc,?char?**argv)

{

?

??MYSQL?*conn;

??MYSQL_RES?*result;

??MYSQL_ROW?row;

??int?num_fields;

??int?i;

?

??conn?=?mysql_init(NULL);

??mysql_real_connect(conn,?"localhost",?"zetcode",?"passwd",?"testdb",?0,?NULL,?0);

?

??mysql_query(conn,?"SELECT?*?FROM?writers");

??result?=?mysql_store_result(conn);

?

??num_fields?=?mysql_num_fields(result);

?

??while?((row?=?mysql_fetch_row(result)))

??{

??????for(i?=?0;?i?<?num_fields;?i++)

??????{

??????????printf("%s?",?row[i]???row[i]?:?"NULL");

??????}

??????printf("\n");

??}

?

??mysql_free_result(result);

??mysql_close(conn);

?

}

The?example?prints?all?names?from?the?writers?table.

$?./select

Leo?Tolstoy

Jack?London

Honore?de?Balzac

Lion?Feuchtwanger

Emile?Zola

?mysql_query(conn,?"SELECT?*?FROM?writers");

We?execute?the?query,?that?will?retrieve?all?names?from?the?writers?database.

?result?=?mysql_store_result(conn);

We?get?the?result?set.

?num_fields?=?mysql_num_fields(result);

We?get?the?number?of?fields?in?the?table.

?while?((row?=?mysql_fetch_row(result)))

?{

?????for(i?=?0;?i?<?num_fields;?i++)

?????{

?????????printf("%s?",?row[i]???row[i]?:?"NULL");

?????}

?????printf("\n");

?}

We?fetch?the?rows?and?print?them?to?the?screen.

?mysql_free_result(result);

We?free?the?resources.

Column?headers

In?the?next?example,?we?will?retrieve?data?and?show?the?their?column?names?from?the?table.

For?this,?we?will?create?a?new?table?friends.

?mysql>?create?table?friends?(id?int?not?null?primary?key?auto_increment,

???????????????????????????????name?varchar(20),?age?int);

?mysql>?insert?into?friends(name,?age)?values('Tom',?25);

?mysql>?insert?into?friends(name,?age)?values('Elisabeth',?32);

?mysql>?insert?into?friends(name,?age)?values('Jane',?22);

?mysql>?insert?into?friends(name,?age)?values('Luke',?28);

We?insert?some?data?into?the?table.

#include?

#include?

?

int?main(int?argc,?char?**argv)

{

?

??MYSQL?*conn;

??MYSQL_RES?*result;

??MYSQL_ROW?row;

??MYSQL_FIELD?*field;

?

??int?num_fields;

??int?i;

?

??conn?=?mysql_init(NULL);

??mysql_real_connect(conn,?"localhost",?"zetcode",?"passwd",?"testdb",?0,?NULL,?0);

?

??mysql_query(conn,?"SELECT?*?FROM?friends");

??result?=?mysql_store_result(conn);

?

??num_fields?=?mysql_num_fields(result);

?

??while?((row?=?mysql_fetch_row(result)))

??{

??????for(i?=?0;?i?<?num_fields;?i++)

??????{

??????????if?(i?==?0)?{

?????????????while(field?=?mysql_fetch_field(result))?{

????????????????printf("%s?",?field->name);

?????????????}

??????????printf("\n");

??????????}

??????????printf("%s??",?row[i]???row[i]?:?"NULL");

??????}

??}

??printf("\n");

?

??mysql_free_result(result);

??mysql_close(conn);

}

The?example?is?similar?to?the?previous?one.?It?just?adds?column?header?names?to?it.

?while(field?=?mysql_fetch_field(result))?{

?????printf("%s?",?field->name);

?}

The?mysql_fetch_field()?call?returns?a?MYSQL_FIELD?structure.?We?get?the?column?header?names?from?this?structure.

$?./headers

id?name?age

1??Tom??25

2??Elisabeth??32

3??Jane??22

4??Luke??28

And?this?is?the?output?of?our?program.

Inserting?p_w_picpaths?into?MySQL?database

Some?people?prefer?to?put?their?p_w_picpaths?into?the?database,?some?prefer?to?keep?them?on?the?file?system?for?their?applications.?Technical?difficulties?arise?when?we?work?with?millions?of?p_w_picpaths.?Images?are?binary?data.?MySQL?database?has?a?special?data?type?to?store?binary?data?called?BLOB?(Binary?Large?Object).

mysql>?describe?p_w_picpaths;

+-------+------------+------+-----+---------+-------+

|?Field?|?Type???????|?Null?|?Key?|?Default?|?Extra?|

+-------+------------+------+-----+---------+-------+

|?id????|?int(11)????|?NO???|?PRI?|?????????|???????|

|?data??|?mediumblob?|?YES??|?????|?NULL????|???????|

+-------+------------+------+-----+---------+-------+

2?rows?in?set?(0.00?sec)

This?is?the?table,?that?we?will?use?in?our?example.?It?can?be?created?by?the?following?SQL?statement.

create?table?p_w_picpaths(id?int?not?null?primary?key,?data?mediumblob);

#include?

#include?

?

int?main(int?argc,?char?**argv)

{

??MYSQL?*conn;

?

??int?len,?size;

??char?data[1000*1024];

??char?chunk[2*1000*1024+1];

??char?query[1024*5000];

?

??FILE?*fp;

?

??conn?=?mysql_init(NULL);

??mysql_real_connect(conn,?"localhost",?"zetcode",?"passwd",?"testdb",?0,?NULL,?0);

?

??fp?=?fopen("p_w_picpath.png",?"rb");

??size?=?fread(data,?1,?1024*1000,?fp);

?

??mysql_real_escape_string(conn,?chunk,?data,?size);

?

??char?*stat?=?"INSERT?INTO?p_w_picpaths(id,?data)?VALUES('1',?'%s')";

??len?=?snprintf(query,?sizeof(stat)+sizeof(chunk)?,?stat,?chunk);

?

??mysql_real_query(conn,?query,?len);

?

??fclose(fp);

??mysql_close(conn);

}

In?this?example,?we?will?insert?one?p_w_picpath?into?the?p_w_picpaths?table.?The?p_w_picpath?can?be?max?1?MB.

?fp?=?fopen("p_w_picpath.png",?"rb");

?size?=?fread(data,?1,?1024*1000,?fp);

Here?we?open?the?p_w_picpath?and?read?it?into?the?data?array.

?mysql_real_escape_string(conn,?chunk,?data,?size);

Binary?data?can?obtain?special?characters,?that?might?cause?troubles?in?the?statements.?We?must?escape?them.?The?mysql_real_escape_string()?puts?the?encoded?data?into?the?chunk?array.?In?theory,?every?character?might?be?a?special?character.?That's?why?the?chunk?array?two?times?as?big?as?the?data?array.?The?function?also?adds?a?terminating?null?character.

?char?*stat?=?"INSERT?INTO?p_w_picpaths(id,?data)?VALUES('1',?'%s')";

?len?=?snprintf(query,?sizeof(stat)+sizeof(chunk)?,?stat,?chunk);

These?two?code?lines?prepare?the?MySQL?query.

?mysql_real_query(conn,?query,?len);

Finally,?we?execute?the?query.

Selecting?p_w_picpaths?from?MySQL?database

In?the?previous?example,?we?have?inserted?an?p_w_picpath?into?the?database.?In?the?following?example,?we?will?select?the?inserted?p_w_picpath?back?from?the?database.

#include?

#include?

?

int?main(int?argc,?char?**argv)

{

??MYSQL?*conn;

??MYSQL_RES?*result;

??MYSQL_ROW?row;

?

??unsigned?long?*lengths;

??FILE?*fp;

?

??conn?=?mysql_init(NULL);

??mysql_real_connect(conn,?"localhost",?"zetcode",?"passwd",?"testdb",?0,?NULL,?0);

?

??fp?=?fopen("p_w_picpath.png",?"wb");

?

??mysql_query(conn,?"SELECT?data?FROM?p_w_picpaths?WHERE?id=1");

??result?=?mysql_store_result(conn);

?

??row?=?mysql_fetch_row(result);

??lengths?=?mysql_fetch_lengths(result);

?

??fwrite(row[0],?lengths[0],?1,?fp);

??mysql_free_result(result);

?

??fclose(fp);

??mysql_close(conn);

}

In?this?example,?we?will?create?an?p_w_picpath?file?from?the?database.

?fp?=?fopen("p_w_picpath.png",?"wb");

We?open?a?file?for?writing.

?mysql_query(conn,?"SELECT?data?FROM?p_w_picpaths?WHERE?id=1");

We?select?an?p_w_picpath?with?id?1.

?row?=?mysql_fetch_row(result);

The?row?contains?raw?data.

?lengths?=?mysql_fetch_lengths(result);

We?get?the?length?of?the?p_w_picpath.

?fwrite(row[0],?lengths[0],?1,?fp);

We?create?the?p_w_picpath?file?using?the?fwrite()?standard?function?call.?

?


轉載于:https://blog.51cto.com/a1liujin/1686202

總結

以上是生活随笔為你收集整理的mysql C语言API接口及实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 黄片毛片 | 青青青手机视频在线观看 | 538国产精品一区二区免费视频 | 双性人hdsexvideos | 亚洲色图美腿丝袜 | 五月天综合久久 | 免费日韩av| 一本一道波多野结衣av黑人 | 一本一道精品欧美中文字幕 | 欧美日本色图 | 黄色网络在线观看 | 熟女少妇内射日韩亚洲 | 九九九国产 | 国产欧美日韩精品在线观看 | 日本作爱视频 | √天堂资源地址在线官网 | 日本视频网站在线观看 | 亚洲国产av一区二区三区 | 人体裸体bbbbb欣赏 | 天天狠狠操 | 青青av | 岛国av在线| 国产第一页第二页 | 欧美亚洲精品在线观看 | 中文字幕在线看 | 久草热在线视频 | www.蜜臀av.com| 级毛片 | 日本我不卡 | 先锋资源av在线 | 人妻视频一区二区 | 国产视频一区二区三区在线观看 | 亚洲精品一区二区三区不卡 | 久久精品视频18 | 国产免费一区视频观看免费 | 亚洲天堂视频网 | 国语对白做受欧美 | 日韩欧美精品在线 | 最新日韩av在线 | 日本不卡视频在线播放 | 国产精品色婷婷 | 国产成人麻豆精品午夜在线 | 一区二区三区视频在线免费观看 | 男人猛吃奶女人爽视频 | 麻豆精品视频免费观看 | 97爱爱视频 | 伦理片一区二区 | 天天插夜夜爽 | 色国产视频 | 超碰人人草人人干 | www.色婷婷.com| 爱情岛论坛亚洲入口 | 韩国三级hd中文字幕有哪些 | 性盈盈影院中文字幕 | 国产精品资源站 | 在线观看网站 | 午夜精品无码一区二区三区 | 日韩爱爱片 | 欧美性俱乐部 | 99中文字幕在线观看 | 国产在线观 | 天天操天天操 | 91免费在线视频观看 | 日韩视频在线一区二区 | 日韩一区二区三区精 | 午夜精品一区 | 红桃视频国产精品 | 污片在线观看 | www.色就是色.com | 外国黄色录像 | 日本a级片免费 | 亚洲视频免费看 | 天天操国产 | 色不卡 | 欧美性大战久久久久久 | 97久久精品人人澡人人爽 | 天天操天天操 | 免费看黄禁片 | 一级特级毛片 | 午夜寂寞影院在线观看 | 亚洲最大成人在线视频 | 四虎免费久久 | 在线中文一区 | 黄色aa级片 | 91在线精品李宗瑞 | 国产男女激情 | 国产 中文 字幕 日韩 在线 | 亚洲爆乳无码一区二区三区 | 成年人av在线 | 免费观看全黄做爰的视频 | xxxxx在线观看 | 精品久久综合 | 在线伊人网 | 天堂中文在线观看 | 国产欧美在线观看不卡 | 黄色精品视频在线观看 | 国产精品久久久久久久专区 | 美女国产免费 | 国产色无码精品视频 |