Current location - Training Enrollment Network - Books and materials - How to set the program header file of reading data in mysql library with C language?
How to set the program header file of reading data in mysql library with C language?
Mysql C API programming steps

1. First, include mysql header files and link mysql dynamic libraries. Add the following statement:

# include & ltWinSock2.h & gt// Network programming needs winsock2.h.

# include & ltmysql.h & gt

#pragma comment (lib, "libmysql.lib")

2. Create MYSQL variables. For example:

MYSQL mysql

3. Initialize MYSQL variables.

MySQL _ init(& amp; MySQL);

4. Call mysql_real_connect function to connect mysql database. The prototype of mysql_real_connect function is as follows:

MYSQL * STDCALL MYSQL _ real _ connect(MYSQL * MYSQL,const char *host,const char *user,const char *passwd,const char *db,unsigned int port,const char *unix_socket,unsigned long client flag);

Parameter description: mysql- mysql variable defined earlier; The address of the host MySQL server; User–login user name; Passwd-- login password; DB–the database to connect to; Port-the TCP service port——MySQL server; Unix _ socket-Unix connection mode; When NULL, it means that socket or pipeline mechanism is not used; Clientflag-Mysql runs as the mark of ODBC database, and generally takes 0. This function returns 0 when the connection fails.

5. Call mysql_real_query function for database query. The prototype of mysql_real_query function is as follows:

Intstdcall MySQL _ real _ query (MySQL * MySQL, const char *q, unsigned long integer);

Parameter description: mysql- mysql variable defined earlier; Q–SQL query statement; Length–The length of the query statement.

If the query is successful, the function returns 0.

6. Get the query result data by calling mysql_store_result or MYSQL_RES variable returned by mysql_use_result function.

The prototypes of these two functions are:

MYSQL _ RES * STDCALL MYSQL _ store _ result(MYSQL * MYSQL);

MYSQL _ RES * STDCALL MYSQL _ use _ result(MYSQL * MYSQL);

These two functions represent two ways to obtain query results respectively. Firstly, the mysql_store_result function is called to store all the data queried from the mysql server to the client, and then read it; Second, call mysql_use_result to initialize the retrieval, thus reading the result set line by line, but it does not read any data from the server itself. This method is faster than the first one and requires less memory, but it will bind the server and prevent other threads from updating any table, and must repeatedly execute mysql_fetch_row to read data until NULL is returned, otherwise unread rows will be returned as part of the results in the next query.

7. Call mysql_fetch_row function to read the result set data.

In the end, both methods call mysql_fetch_row function repeatedly to read data. The prototype of mysql_fetch_row function is as follows:

MYSQL _ ROW STDCALL MYSQL _ fetch _ ROW(MYSQL _ RES * result);

Parameter result is the return value of mysql_store_result or mysql_use_result.

This function returns a MYSQL_row variable, which is an array of strings. Suppose ROW, ROW [i] is the value of the ith field. When the end of the result set is reached, the function returns NULL.

8. When the result set is used up, call mysql_free_result function to release the result set to prevent memory leakage. The prototype of mysql_free_result function is as follows:

void STDCALL MYSQL _ free _ result(MYSQL _ RES * result);

9. When the Mysql database is no longer queried, call the mysql_close function to close the database connection. The prototype of mysql_close function is:

void STDCALL MYSQL _ close(MYSQL * sock);