Article From:

2. Research Background

Problem definition:This paper focuses on the data operation of MySQL database, and studies the basic API of operating MySQL database in C language, including the basic definition of tables in MySQL database and common database operations such as add, delete, modify and query.

difficulty:Skilled in application

Related work: C Language database MySQL

Three Research Contents

C Relevant Functions for Language Access to MYSQL Database

MYSQL *mysql_init(MYSQL *mysql)
Initialize MYSQL instanceVoid mysql_close (MYSQL * mysql)Close the MYSQL instance and release memoryInt mysql_query (MYSQL * mysql, constchar)* query) Query the specified connection. Successful return of 0, error return of non-zero valueMYSQL_RES * musql_store_ result (MYSQL * handle) returns the result of the queryMYSQL_ROW mysql_fetch_ row (MYSQL_RES*res) returns the results of the first row of queries

CProgram Example of Language Access to MySQL

#include <stdio.h> #include <stdlib.h> #include <mysql/mysql.h>
// Define database query macros (here based on parameters)Userid queries the name year attribute of the teacher table)# define SELECT_QUERY "select name, year from teach"Er where userid =% d"//main function, the entry to C language executionInt main (int argc, char ** argv) {// Define the variables used// Call mysql_iNit method, initialize connection instance mysql_init (& amp; mysql);// / Insert data into the database (insert operation) and return in case of exceptionsRes = mysql_query (& amp; my_conn)"INSERT INT O teacher (name, year) VALUES ('Ana', 1989)";If (RES)Fprintf (stderr, "Insert er"Ror% d:% s n ", mysql_errno (& amp; my_connection), mysql_ error (& amp; my_connection);}// Modify data (update)Operations, if abnormalReturnRes = mysql_query (& amp; my_connection, "update teacher set year= 1999 where name='PetEr'"";If (RES)Fprintf (stderr,'Update error% d:% s n', mysql_errno (& amp; my_connection), mysql_ eRror (& amp; my_connection);}Sprintf (buf, SELECT_QUERY, ATOI (argv[1]);// Query data rows (se)Lect operation)If (mysql_query (handle, buf){fprintf (stderr), `Query failed (% s)\ n', mysql_Error (handle); eXit (1);}// Cache resultsIf (!) (result = mysql_store_result (handle)) {fprintf (stderr,""Get result from% s fa"Iled\n, "Mysql_error (handle); exit (1);}Printf ("number of fields returned:% d n ", mysql_num_fieldS (result); query ok!

Four conclusions

Access to MySQL in C language is not very common in actual Web applications, but sometimes in order to achieve higher access performance, we will directly use C language to develop relevant database access module, which can make better use of server resources, and use C language has better access performance.Security, so using C language to access MySQL database is more expensive than other languages in program development, but it has better performance and security.


[1] Wu Yuan, Yu Jin, Guo Lei, et al. Transplantation and application of MySQL database on embedded Linux platform [J]. Electronic technology, 2011, 38 (07): 12-13.

[2] Zhao Hongming. Programming for accessing database in MySQL [J]. Journal of Huangshi College, 2012, 18 (01): 10-12.

[3] Li Da. Design and Implementation of High Resolution Remote Sensing Image Segmentation System Based on Knowledge Base [D]. Taiyuan: Shanxi University, 2014.

Link of this Article: Document Notes (9)

Leave a Reply

Your email address will not be published. Required fields are marked *