1, C and MySQL
Because MySQL is developed in C language, there will be a series of APIs that can be called;
2, C call the basic model of 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); //initialization 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) { //connection 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;
To run the command:
GCC Dm01_hello.c-o dm01_hello-i/usr/include-l/usr/lib64/mysql-lmysqlclient-lm-lrt-ldl-lstdc++-lpthread
Operation Result:
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 query MySQL
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <mysql/mysql.h>/* * Chinese question *mysql_query query *mysql_store_result get handle * *locate mysql.h can find the directory where the. h file is located * * */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,&NBSP;0) //The resolution of the Chinese problem mysql_set_character_set (&mysql, "UTF8"); if (Connect == null) { ret = mysql_errno (&mysql); printf ("Func mysql_real_connect () err\n "); return ret; } //Query 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; } //get result set and //result set and may contain multiple rows of data, get result set //mysql_store_result design idea: Tell a handle, All of a sudden I take the data from the server to the client, and then cache it up mysql_res *result = mysql_store_result (& MySQL); Get results from the server side during //use //MYSQL_RES *result = Mysql_use_result (&mysql); //can get the number of elements in this table per row in this database unsigned int num = mysql_field_count (&mysql); int i; mysql_row row = null; //can see in mysql.h //Print Header mysql_field *fields = mysql_fetch_fields (result ); for (i = 0; i < num; i++) { printf ("%s\t", fields[i].name); } printf ("\ n"); //Print the contents of the table while (Row = mysql_fetch_row (result)) { for (i = 0; i < num; i++) { printf ("%s\t", row[i]); } printf ("\ n"); }/* * here is how many elements we see in this table row 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; }
Operation Result:
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 development MySQL Client
Only the functions of the query are implemented:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <mysql/mysql.h>int Main (INT&NBSP;ARGC,&NBSP;CHAR&NBSP;**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", &NBSP;ARGV[1],&NBSP;0,&NBSP;NULL,&NBSP;0); //the solution of Chinese problem 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>&NBSP;:"); &NBSP;//SCANF () statement to tab space Enter are omitted, the SQL statement will be truncated, with get () to maintain the SQL statement is the same nature gets (SQLBUF); //exit if (strncmp ("Exit", sqlbuf, 4) == 0 | | &NBSP;STRNCMP ("Quit", sqlbuf, 4) == 0) { break; } //whether the query is a SQL statement //ret = mysql_query (&mysql, "Set name utf8"); ret = mysql_query ( &MYSQL,&NBSP;SQLBUF); if (Ret != null) { ret = mysql_errno (&mysql); printf ("Func mysql_query () err\n "); return ret; } if (strncmp ("select", &NBSP;SQLBUF,&NBSP;6) == 0 | | &NBSP;STRNCMP ("select", sqlbuf, 6) == 0) {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; mysql_res *result = mysql_store_result (&mysql); unsigned int num = mysql_field_count (&mysql); //How many columns are there in the header int i; mysql_row row = null; //can see in mysql.h //Print Header mysql_field * Fields = mysql_fetch_fields (Result); for (i = 0; i < num; i++) { //Print header printf ("%s\t", fields[i].name); } printf ("\ n"); //Print table contents 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;}
Look at the Mysql.h file:
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 "/>
can know: The real type of Mysql_row: char * * ;
Operation Result:
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 "/>
Look at 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 "/>
Because of the client's C language development database, I only implemented the query function, the other functions are not implemented, resulting in no printing, but now can be done through the client to the database operation;
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 "/>
This article is from the "11586096" blog, please be sure to keep this source http://11596096.blog.51cto.com/11586096/1867218
C Developing MySQL Client