Student Information Management in Mysql c api, mysqlapi
Zookeeper
With the embedded MySQL Server library, you can use MySQL servers with all the features in client applications. The main advantage is that it increases the speed and simplifies the management of embedded applications.
The Embedded Server library is based on the MySQL client/server version and is written in C/C ++. The result is that the Embedded Server is also written in C/C ++. In other languages, the Embedded Server is unavailable.
The API is equivalent to the embedded MySQL version and the client/server version. To change the old line program application to use the embedded library, you only need to add calls to the following functions normally.
When interacting with MySQL, the application should use this general principle:
1. Call mysql_library_init () to initialize the MySQL database. The library can be the mysqlclient C client library or the mysqld Embedded Server library, depending on whether the application is linked to the "-libmysqlclient" or "-libmysqld" flag.
2. Call mysql_init () to initialize the connection handler and call mysql_real_connect () to connect to the server.
3. Issue an SQL statement and process the result. (The method for using it is described in detail in the following discussion ).
4. Call mysql_close () to close the connection to the MySQL server.
5. Call mysql_library_end () to end using the MySQL database.
The purpose of calling mysql_library_init () and mysql_library_end () is to provide proper initialization and end processing for the MySQL database. Applications linked to the client library provide improved memory management functions. If mysql_library_end () is not called, the memory block remains allocated (this does not increase the amount of memory used by the application, but some memory leak detectors will protest against it ). For applications linked to an Embedded Server, these calls start and stop the server.
Mysql_library_init () and mysql_library_end () are actually # define symbols, which make them equivalent to mysql_server_init () and mysql_server_end (), but their names are more clearly indicated, whether the application uses the mysqlclient or mysqld library, it should be called when the MySQL library is started or ended. For earlier MySQL versions, replace mysql_server_init () and mysql_server_end.
If you want to, you can omit the call to mysql_library_init (). This is because, if necessary, mysql_init () will automatically call it.
To connect to the server, call mysql_init () to initialize the connection handler, and then call mysql_real_connect () using the handler (and other information such as the host name, user name, and password (). After a connection is established, mysql_real_connect () will set the reconnect flag (part of the MYSQL structure) to 1 or in a newer version of the API earlier than 5.0.3, set it to 0. For this flag, the value "1" indicates that if the statement cannot be executed due to connection loss, the system tries to connect to the server again before giving up. From MySQL 5.0.13, you can use the MYSQL_OPT_RECONNECT option on mysql_options () to control reconnection. Call mysql_close () to stop the connection.
When the connection is active, the client may use mysql_query () or mysql_real_query () to issue an SQL query to the server. The difference between the two lies in that the mysql_query () Expectation query is the specified string ending with Null, while mysql_real_query () expectation is the count string. If a string contains binary data (which may contain Null bytes), mysql_real_query () must be used ().
For each non-SELECT query (such as INSERT, UPDATE, and DELETE), you can find the number of rows that have been changed (affected) by calling mysql_affected_rows ).
For SELECT queries, you can retrieve rows that are used as result sets. Note that some statements return rows, similar to SELECT statements. Including SHOW, DESCRIBE, and EXPLAIN. The SELECT statements should be treated as follows.
The client can process result sets in two ways. One way is to call mysql_store_result () to retrieve the entire result set at a time. This function can retrieve all rows returned by the query from the server and save them on the client. The second method is for the client. By calling mysql_use_result (), the "by row" result set retrieval is initialized. This function can initialize the search results, but cannot obtain any actual rows from the server.
In both cases, you can access the row by calling mysql_fetch_row. Through mysql_store_result (), mysql_fetch_row () can access the rows previously obtained from the server. Through mysql_use_result (), mysql_fetch_row () can actually retrieve rows from the server. You can call mysql_fetch_lengths () to obtain information about the data size in each row.
After the result set operation is completed, call mysql_free_result () to release the memory used by the result set.
These two retrieval mechanisms complement each other. The client program should select the method that best meets its requirements. In fact, mysql_store_result () is the most commonly used client ().
One advantage of mysql_store_result () is that because you extract all rows to the client, you can not only access the rows consecutively, but also use mysql_data_seek () or mysql_row_seek () move forward or backward in the result set to change the position of the current row in the result set. By calling mysql_num_rows (), how many lines can be found. On the other hand, for large result sets, the memory required for mysql_store_result () may be large, and you may encounter memory overflow.
One advantage of mysql_use_result () is that the client requires less memory for the result set, because it only maintains one row at a time (due to low allocation overhead, mysql_use_result () faster ). Its disadvantage is that you must quickly process each row to avoid interfering with the server. You cannot randomly access the rows in the result set (only consecutive rows can be accessed). You do not know how many rows are in the result set, until all of them are retrieved. In addition, You must retrieve all rows even if you have found the information you are searching.
Through the API, the client can properly respond to the query (only retrieve rows if necessary) without knowing whether the query is a SELECT query. You can call mysql_store_result () to complete this operation after mysql_query () or mysql_real_query. If the result set is successfully called, the query is SELECT and the row can be read. If the call to the result set fails, you can use mysql_field_count () to determine whether the result is expected. If mysql_field_count () returns 0, no data is returned for the query (it indicates it is INSERT, UPDATE, DELETE, etc.), and no rows are returned. If mysql_field_count () is not 0, the query returns rows, but no rows. This indicates that the query failed SELECT. For examples of how to implement this operation, see Introduction to mysql_field_count.
Whether mysql_store_result () or mysql_use_result (), you can obtain information about the fields that constitute the result set (number of fields, their names and types ). By repeatedly calling mysql_fetch_field (), you can access field information in the row in sequence, or by calling mysql_fetch_field_direct (), you can access field information by field number in the row. You can change the cursor position of the current field by calling mysql_field_seek. Setting the field cursor will affect subsequent mysql_fetch_field () calls. In addition, you can obtain all information about fields at a time by calling mysql_fetch_fields.
To detect and report errors, MySQL provides a mechanism to access error information using the mysql_errno () and mysql_error () functions. They can return error code or error messages about the recently called function, and the recently called function may succeed or fail. In this way, you can determine when an error occurs, and the error.
Student Information Management System of CAPI in Mysql
First, I performed an experiment in fedora-8.
Install the library required by mysql under/usr/lib and the header file under/usr/include/mysql. h.
Use the c api in mysql to manage a single table for student information.
Some source code:
Void Show (char Table_Name []) // output table content {char str [25]; sprintf (str, "select * from % s", Table_Name); mysql_real_query (mysql, str, (unsigned int) strlen (str); results = mysql_store_result (mysql); printf ("\ n"); while (field = mysql_fetch_field (results ))) {printf ("% s \ t", field-> name);} printf ("\ n"); while (record = mysql_fetch_row (results ))) {num_fields = mysql_num_fields (results); for (I = 0; I <num_fields; I ++) {printf ("% s \ t ", record [I]);} printf ("\ n");} mysql_free_result (results );}
Void Insert (char Table_Name []) // Add information {char str1 [4] [10]; char str [30]; printf ("Enter the name :"); scanf ("% s", str1 [0]); printf ("Enter your student ID:"); scanf ("% s", str1 [1]); printf ("Enter your math score:"); scanf ("% s", str1 [2]); printf ("Enter your Chinese score :"); scanf ("% s", str1 [3]); sprintf (str, "Insert into % s values (\" % s \ ", \" % s \", \ "% s \", \ "% s \") ", Table_Name, str1 [0], str1 [1], str1 [2], str1 [3]); printf ("\ n >>> % s \ n", str); mysql_real_query (mysql, str, (unsi Gned int) strlen (str); printf (">>> added successfully !!! \ N ");}
Void Delete (char Table_Name []) // Delete information {char Delete_Name [10]; char Delete_Str [40]; printf ("enter the name of the information to be deleted :"); scanf ("% s", Delete_Name); sprintf (Delete_Str, "Delete from % s where name = \" % s \ "", Table_Name, Delete_Name ); printf ("\ n >>> % s \ n", Delete_Str); mysql_real_query (mysql, Delete_Str, (unsigned int) strlen (Delete_Str )); printf (">>> deleted successfully !!! \ N ");}
Void Find (char Table_Name []) // modify the information {char Find_Str [40]; char Find_Name [10]; printf ("select the name to query :"); scanf ("% s", Find_Name); sprintf (Find_Str, "select * from % s where name = \" % s \ "", Table_Name, Find_Name ); printf ("\ n >>> % s \ n", Find_Str);/* run the SQL query */mysql_real_query (mysql, Find_Str, (unsigned int) strlen (Find_Str); results = mysql_store_result (mysql ); // access the row printf (">>> % d messages \ n", * results) obtained from the server ); while (field = mysql_fetch_field (results) {printf ("% s \ t", field-> name);} printf ("\ n "); while (record = mysql_fetch_row (results) // can access the rows previously obtained from the server {num_fields = mysql_num_fields (results); for (I = 0; I <num_fields; I ++) {printf ("% s \ t", record [I]);} printf ("\ n ");} printf ("\ n ");}
The above is only part of the simple functions, there are more functions please refer to: http://download.csdn.net/detail/u013930856/8377673