自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

用c語言正確讀取MySQL數(shù)據(jù)庫實戰(zhàn)演示

數(shù)據(jù)庫 MySQL
此文章主要描述的用c語言正確讀取MySQL數(shù)據(jù)庫的實際操作步驟,以下就是文章的詳細(xì)內(nèi)容講解,希望會給你帶來一些幫助在此方面。

以下的文章主要向大家描述的是用c語言正確讀取MySQL數(shù)據(jù)庫的實際操作流程,如果你對用c語言正確讀取MySQL數(shù)據(jù)庫的正確操作流程感興趣的話,那么以下的文章就會滿足你的好奇之心了。

最近一段時間我們學(xué)習(xí)了linux操作系統(tǒng)下的C開發(fā),呵呵,寫了一個測試程序,作用是讀取MySQL的數(shù)據(jù),然后顯示出來。經(jīng)測試成功…

  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3. #include <MySQL.h> 
  4. #define DB_SERVER ""  
  5. #define DB_NAME "test"  
  6. #define DB_USER "root"  
  7. #define DB_PWD ""  
  8. static MySQL *db_handel,MySQL;  
  9. static MySQL_ROW row;  
  10. static int query_error;  
  11. MySQL_RES *query_test(char *sql);  
  12. int query_show(MySQL_RES *result);  
  13. int main(int argc,char *argv[])  
  14. {  
  15. MySQL_RES * results;  
  16. results=query_test("select * from test");  

獲取記錄

  1. query_show(results); 

顯示記錄

  1. return 0;  

查詢記錄

  1. MySQL_RES *query_test(char *sql)  
  2. {  
  3. static MySQL_RES *query_result;  
  4. printf("%s\n",sql);  
  5. MySQL_init(&MySQL);  
  6. db_handel=MySQL_real_connect(&MySQL,DB_SERVER,DB_USER,DB_PWD,DB_NAME,0,0,0); 

打開讀取MySQL數(shù)據(jù)庫連接

  1. if(db_handel==NULL) 

錯誤處理

{

 

  1. printf(MySQL_error(&MySQL));  
  2. return NULL;  
  3. }  
  4. query_error=MySQL_query(db_handel,sql);  

查詢

  1. if(query_error!=0) 

錯誤處理

  1. {  
  2. printf(MySQL_error(db_handel));  
  3. return NULL;  
  4. }  
  5. query_result=MySQL_store_result(db_handel); 

獲取記錄

  1. MySQL_close(db_handel); 

關(guān)閉數(shù)據(jù)庫

  1. return query_result; 

返回記錄

 

 

顯示記錄

 

 

  1. int query_show(MySQL_RES *result)  
  2. {  
  3. unsigned int i,num_fields;  
  4. MySQL_FIELD *fileds;  
  5. num_fields=MySQL_num_fields(result);  

 

獲取字段數(shù)

 

 

  1. fileds=mysql_fetch_fields(result); 

獲取字段數(shù)組

 

 

  1. while((row=mysql_fetch_row(result))!=NULL) 

循環(huán)顯示

 

 

  1. {  
  2. for(i=0;i<num_fields;i++)  
  3. {  
  4. printf("%s: %s \n",fileds[i].name,row[i]?row[i]:"NULL");  
  5. }  
  6. }  
  7. return 0;  

 

以下是MakeFile文件內(nèi)容

  1. CC=gcc 
  2. #LDLIBS=`gtk-config --libs --cflags`  
  3. LDLIBS=-L /usr/lib/mysql -I /usr/include/mysql -l mysqlclient  
  4. CFLAGS=-Wall -g`gtk-config --cflags`  
  5. window:window.c  
  6. $(CC) $(LDLIBS) window.c -o window  
  7. #window.o:window.c  
  8. # $(CC) $(LDLIBS) -c window.c  
  9. clean:  
  10. rm -f window  
  11. rm -f *.o  

以上的相關(guān)內(nèi)容就是對c語言讀取Mysql的介紹,望你能有所收獲。

【編輯推薦】

  1. MySQL 資源的正確應(yīng)用
  2. MySQL數(shù)據(jù)庫的基本操作演示
  3. MySQL 免安裝版的實際配置方法
  4. 重置MySQL數(shù)據(jù)庫密碼的實際操作流程
  5. MySQL簡單命令概述

 

責(zé)任編輯:佚名 來源: 博客園
相關(guān)推薦

2010-06-04 09:33:28

連接MySQL數(shù)據(jù)庫

2010-06-30 08:46:51

升級SQL Serve

2010-05-18 11:04:11

MySQL數(shù)據(jù)庫

2010-06-04 11:21:41

連接MySQL數(shù)據(jù)庫

2010-05-28 14:51:47

MySQL數(shù)據(jù)庫

2010-06-09 17:36:45

MySQL數(shù)據(jù)庫同步

2010-06-01 12:51:23

MySQL數(shù)據(jù)庫

2010-05-12 18:41:34

MySQL數(shù)據(jù)庫

2010-06-12 15:53:22

MySQL數(shù)據(jù)庫

2010-05-05 11:17:55

Oracle數(shù)據(jù)庫

2010-07-29 09:14:57

DB2數(shù)據(jù)庫表

2010-08-12 11:38:21

DB2數(shù)據(jù)庫

2011-04-26 10:37:04

C++MySQL

2010-05-27 16:55:23

操作MySQL

2010-08-05 13:19:48

連接DB2數(shù)據(jù)庫

2010-06-02 11:34:23

MySQL 數(shù)據(jù)庫導(dǎo)入

2010-05-14 10:39:32

MySQL數(shù)據(jù)庫

2011-04-26 10:24:27

C++MySQL

2010-06-02 16:20:08

2010-08-05 13:54:26

連接DB2數(shù)據(jù)庫
點贊
收藏

51CTO技術(shù)棧公眾號