How to use C language to correctly read MySQL database

Source: Internet
Author: User

The following articles mainly describe the actual operation process of reading the MySQL database in C language. If you are interested in the correct operation process of reading the MySQL database in C language, the following articles will satisfy your curiosity.

Recently, we learned about C development in the linux operating system. Haha, I wrote a test program to read MySQL Data and display it. Tested successfully...

 
 
  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");  

Retrieve records

 
 
  1. query_show(results); 

Show records

 
 
  1. return 0;  

Query records

 
 
  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); 

Open the connection to read the MySQL database

 
 
  1. if(db_handel==NULL) 

Error Handling

{

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

Query

 
 
  1. if(query_error!=0) 

Error Handling

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

Retrieve records

 
 
  1. MySQL_close(db_handel); 

Close Database

 
 
  1. return query_result; 

Return record

 
 

Show records

 
 
  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);  

Obtain the number of fields

 
 
  1. fileds= mysql _fetch_fields(result); 

Get field array

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

Loop display

 
 
  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;  

The contents of MakeFile are as follows:

 
 
  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  

The above content is an introduction to reading Mysql from the C language. I hope you will get some benefits.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.