標籤:c開發資料庫mysql
1、C與Mysql
因為Mysql是用C語言開發的,所以會有一系列的API可以調用;
2、C調用Mysql的基本模型
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<mysql/mysql.h>int main(void){ int ret = 0; MYSQL mysql; MYSQL *connect = NULL; connect = mysql_init(&mysql); //初始化 if(connect == NULL){ ret = -1; printf("func mysql_init() err\n"); return ret; } connect = mysql_real_connect(connect, "localhost", "root", "123456", "mydb1", 0, NULL, 0); if(connect == NULL){ //串連mysql ret = -1; printf("func mysql_real_connect() err\n"); return ret; } printf("func mysql_real_connect() ok\n"); mysql_close(&mysql); printf("hello world\n"); return ret;
運行命令:
gcc dm01_hello.c -o dm01_hello -I/usr/include -L/usr/lib64/mysql -lmysqlclient -lm -lrt -ldl -lstdc++ -lpthread
運行結果:
650) this.width=650;" src="http://s1.51cto.com/wyfs02/M02/89/78/wKioL1gUnSORwAnMAABIDYX63jY597.png-wh_500x0-wm_3-wmp_4-s_3611081132.png" title="QQ20161029205915.png" alt="wKioL1gUnSORwAnMAABIDYX63jY597.png-wh_50" />
3、C查詢Mysql
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<mysql/mysql.h>/* *中文問題 *mysql_query 查詢 *mysql_store_result 擷取控制代碼 * *locate mysql.h 可以尋找這個.h檔案所在的目錄 * * */int main(void){ int ret = 0; MYSQL mysql; MYSQL *connect = NULL; connect = mysql_init(&mysql); if(connect == NULL){ ret = mysql_errno(&mysql); printf("func mysql_init() err\n"); return ret; } connect = mysql_real_connect(connect, "localhost", "root", "123456", "mydb1", 0, NULL, 0); //中文問題的解決 mysql_set_character_set(&mysql, "utf8"); if(connect == NULL){ ret = mysql_errno(&mysql); printf("func mysql_real_connect() err\n"); return ret; } //查詢 const char *query = "select * from student"; ret = mysql_query(&mysql, query); if(ret != NULL){ ret = mysql_errno(&mysql); printf("func mysql_query() err\n"); return ret; } //擷取結果集和 //結果集和中可能含有多行資料,擷取結果集 //mysql_store_result設計理念:告訴控制代碼,我一下子全部把資料從伺服器端取到用戶端,然後緩衝起來 MYSQL_RES *result = mysql_store_result(&mysql); //使用的過程中從伺服器端擷取結果 //MYSQL_RES *result = mysql_use_result(&mysql); //可得該資料庫中這張表每行有多少元素 unsigned int num = mysql_field_count(&mysql); int i; MYSQL_ROW row = NULL; //在mysql.h中可以看到 //列印表頭 MYSQL_FIELD *fields = mysql_fetch_fields(result); for(i = 0; i < num; i++){ printf("%s\t", fields[i].name); } printf("\n"); //列印表中內容 while(row = mysql_fetch_row(result)){ for(i = 0; i < num; i++){ printf("%s\t", row[i]); } printf("\n"); }/* * 這裡是我們自己看到該表一行有多少元素 while(row = mysql_fetch_row(result)){ printf("%s, %s, %s, %s, %s, %s\n", row[0], row[1], row[2], row[3], row[4], row[5]); }*/ mysql_free_result(result); mysql_close(&mysql); printf("hello world\n"); return ret; }
運行結果:
650) this.width=650;" src="http://s4.51cto.com/wyfs02/M00/89/7B/wKiom1gUnnHyj80eAAAwRdqzXsg760.png-wh_500x0-wm_3-wmp_4-s_2403334288.png" title="QQ20161029210441.png" alt="wKiom1gUnnHyj80eAAAwRdqzXsg760.png-wh_50" />
4、C開發Mysql用戶端
只實現了查詢的功能:
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<mysql/mysql.h>int main(int argc, char **argv){ int ret = 0; MYSQL mysql; MYSQL *connect = NULL; char sqlbuf[80]; connect = mysql_init(&mysql); if(connect == NULL){ ret = mysql_errno(&mysql); printf("func mysql_init() err\n"); return ret; } connect = mysql_real_connect(connect, "localhost", "root", "123456", argv[1], 0, NULL, 0); //中文問題的解決 mysql_set_character_set(&mysql, "utf8"); if(connect == NULL){ ret = mysql_errno(&mysql); printf("func mysql_real_connect() err\n"); return ret; } for(;;){ memset(sqlbuf, 0, sizeof(sqlbuf)); printf("mysql> :"); //scanf()語句對tab 空格 斷行符號 都省去了,對sql語句將會發生截斷,用gets()可保持sql語句的原樣性 gets(sqlbuf); //退出 if(strncmp("exit", sqlbuf, 4) == 0 || strncmp("quit", sqlbuf, 4) == 0){ break; } //查詢是否為SQL語句 //ret = mysql_query(&mysql, "set name utf8"); ret = mysql_query(&mysql, sqlbuf); if(ret != NULL){ ret = mysql_errno(&mysql); printf("func mysql_query() err\n"); return ret; } if(strncmp("select", sqlbuf, 6) == 0 || strncmp("SELECT", sqlbuf, 6) == 0){ MYSQL_RES *result = mysql_store_result(&mysql); unsigned int num = mysql_field_count(&mysql); //表頭有多少列 int i; MYSQL_ROW row = NULL; //在mysql.h中可以看到 //列印表頭 MYSQL_FIELD *fields = mysql_fetch_fields(result); for(i = 0; i < num; i++){ //列印表頭 printf("%s\t", fields[i].name); } printf("\n"); //列印表中內容 while(row = mysql_fetch_row(result)){ for(i = 0; i < num; i++){ printf("%s\t", row[i]); } printf("\n"); } mysql_free_result(result); } } mysql_close(&mysql); printf("hello world\n"); return ret;}
看看mysql.h檔案:
650) this.width=650;" src="http://s4.51cto.com/wyfs02/M02/89/7B/wKiom1gUoKeBbqk5AABT4rjNPHY729.png-wh_500x0-wm_3-wmp_4-s_708438074.png" title="QQ20161029211307.png" alt="wKiom1gUoKeBbqk5AABT4rjNPHY729.png-wh_50" />
650) this.width=650;" src="http://s1.51cto.com/wyfs02/M02/89/78/wKioL1gUoOuAno9pAAA6JSDULXQ727.png-wh_500x0-wm_3-wmp_4-s_3484434084.png" title="QQ20161029211522.png" alt="wKioL1gUoOuAno9pAAA6JSDULXQ727.png-wh_50" />
可以知道:MYSQL_ROW的真實類型:char **;
運行結果:
650) this.width=650;" src="http://s1.51cto.com/wyfs02/M00/89/78/wKioL1gUox2jKWqPAAAXN3Kj0ug507.png-wh_500x0-wm_3-wmp_4-s_3871958417.png" title="QQ20161029212440.png" alt="wKioL1gUox2jKWqPAAAXN3Kj0ug507.png-wh_50" />
看看Mysql:
650) this.width=650;" src="http://s5.51cto.com/wyfs02/M01/89/7B/wKiom1gUo1GyU5OgAAAjPMwIfGo421.png-wh_500x0-wm_3-wmp_4-s_3020289797.png" title="QQ20161029212536.png" alt="wKiom1gUo1GyU5OgAAAjPMwIfGo421.png-wh_50" />
650) this.width=650;" src="http://s2.51cto.com/wyfs02/M01/89/78/wKioL1gUpETDyWADAABDhXViPmY065.png-wh_500x0-wm_3-wmp_4-s_1303315886.png" title="QQ20161029212928.png" alt="wKioL1gUpETDyWADAABDhXViPmY065.png-wh_50" />
由於用戶端的C語言開發資料庫,我只實現了查詢功能,其他的功能沒有實現,導致沒有列印出來,但是現在已經可以通過這個用戶端對資料庫進行操作了;
650) this.width=650;" src="http://s5.51cto.com/wyfs02/M01/89/7B/wKiom1gUprahM0_TAABY419WU3o328.png-wh_500x0-wm_3-wmp_4-s_588497764.png" title="QQ20161029214003.png" alt="wKiom1gUprahM0_TAABY419WU3o328.png-wh_50" />
本文出自 “11586096” 部落格,請務必保留此出處http://11596096.blog.51cto.com/11586096/1867218
C開發Mysql用戶端