Student Information Management for C API in Mysql

Source: Internet
Author: User
Tags sprintf



Using the embedded MySQL server library, you can use a MySQL server with full features in a client application. The main advantage is that it increases speed and makes the management of embedded applications easier.

The embedded server library is based on the MySQL client/server version and is written in C + + language. The result is that the embedded server is also written in C + + language. In other languages, the embedded server is not available.

The API is equivalent to the embedded MySQL version and the client/server version. To change the old thread application to use an embedded library, you normally simply add a call to the following function.

When interacting with MySQL , the application should use this general principle:

1. Initialize the MySQL library by calling mysql_library_init () . The library can be a mysqlclient C client library, or mysqld an embedded server library, depending on whether the application is associated with "-libmysqlclient" or " -libmysqld " flag link.

2. Initialize the connection handler by calling Mysql_init () and connect to the server by calling Mysql_real_connect () .

3. Issue the SQL statement and process its results. (in the following discussion, the method of using it is described in detail).

4. Close the connection to the MySQL server by calling mysql_close () .

5. End the use of the MySQL library by calling mysql_library_end () .

The purpose of calling Mysql_library_init () and mysql_library_end () is to MySQL The library provides proper initialization and end processing. For applications that are linked to the client library, they provide improved memory management capabilities. If you do not call mysql_library_end (), the memory block will remain allocated (this will not increase the amount of memory used by the application, but some memories leak detectors will protest it). For applications that are 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 whose name is more clearly indicated, whether the application is using a mysqlclient or mysqld Library, start or end MySQL Library, you should call them. for earlier versions of MySQL , you can call Mysql_server_init () and mysql_server_end () instead.

If you wish, you can omit the call to Mysql_library_init () because, if necessary,Mysql_init () will call it automatically.

To connect to the server, call mysql_init () to initialize the connection handler, and then call Mysql_real_connect ()with the handler (and other information, such as host name, user name, and password). After the connection is established,Mysql_real_connect () sets the re-connect flag (part of theMySQL structure ) to the 5.0.3 version of the API below 1, or in a newer version, set it to 0. For this flag, the value "1" indicates that if the statement cannot be executed because of a loss of connection, it will attempt to connect to the server again before discarding. Starting with MySQL 5.0.13 , you can use the mysql_opt_reconnect option on mysql_options () to control the reconnection behavior. When the connection is complete, call mysql_close () to abort it.

When the connection is active, the client may use mysql_query () or mysql_real_query () to issue a SQL query to the server. The difference between the two is thatmysql_query () expects the query to be the specified, Null -terminated string, and mysql_real_query () The expected count string. If the string contains binary data (which may contain Null bytes), you must use mysql_real_query ().

For each non- SELECT query (for example, INSERT,UPDATE,DELETE), by calling Mysql_affected_rows () , you can see how many rows have been changed (affected).

For a SELECT query, You can retrieve the rows that are the result set. Note that some statements are similar to SELECT because they return rows . includes SHOW,DESCRIBE and EXPLAIN. They should be treated in the same way as the SELECT statement.

There are two ways that a client handles a result set. One way is to retrieve the entire result set at once by calling Mysql_store_result (). The function can get all the rows returned by the query from the server and save them on the client. The second approach is for the client, by calling Mysql_use_result (), to initialize the " by-row " result set retrieval. The function can initialize the results of the search, but it cannot get any actual rows from the server.

In both cases, the row can be accessed by calling mysql_fetch_row () . With Mysql_store_result (),mysql_fetch_row () can access rows that were previously obtained from the server. With Mysql_use_result (),mysql_fetch_row () is able to actually retrieve rows from the server. by calling Mysql_fetch_lengths (), you can get information about the size of the data in each row.

After you complete the result set operation, call Mysql_free_result () to release the memory used by the result set.

These two search mechanisms are complementary to each other. The client program should choose the method that best meets its requirements. In fact, Mysql_store_result ()is most commonly used by clients.

The 1 advantage of Mysql_store_result () is that you can not only continuously access the rows, but also use mysql_data_seek () or Mysql_row_seek () moves forward or backward in the result set to change the position of the current row within the result sets. by calling Mysql_num_rows (), you can also see how many rows are there. On the other hand, for large result sets, the memory required forMysql_store_result () can be large and you are likely to experience a memory overflow condition.

The 1 advantage of Mysql_use_result () is that the client needs less memory for the result set because it maintains only one row at a time (due to the lower allocation overhead,mysql_ Use_result () can be faster ). The downside is that you have to deal with each line quickly to avoid interfering with the server, you can't randomly access rows in the result set (you can only access rows consecutively), and you don't know how many rows are in the result set until you retrieve them all. Not only that, you must also retrieve all the rows, even if you decide that you have found the information you are looking for during the retrieval process.

Through the API , the client can respond appropriately to the query (retrieving rows only if necessary) without knowing whether the query is a SELECT query. You can call Mysql_store_result () after each mysql_query () or mysql_real_query () The completes the operation. If the result set is called successfully, the query is SELECT , and the row can be read. If the result set call fails, you can call Mysql_field_count () to determine if the result is indeed expected. If mysql_field_count () returns 0 , the query does not return data (indicating that it is INSERT , UPDATE , DELETE , and so on), and do not return rows. If mysql_field_count () is a non- 0 value, the query should return rows, but no rows are returned. This indicates that the query was a SELECT that failed. For an example of how to do this, see the introduction to Mysql_field_count () .

Both Mysql_store_result () and Mysql_use_result ()allow you to get information about the fields that comprise the result set (number of fields, their names and types, and so on). By repeatedly calling Mysql_fetch_field (), you can sequentially access the field information within the row, or, by calling Mysql_fetch_field_direct (), Ability to access field information by field number within a row. by calling Mysql_field_seek (), you can change the cursor position of the current field. The setting of the field cursor will affect subsequent Mysql_fetch_field () calls. In addition, you can get all the information about a field at once by calling Mysql_fetch_fields ().

To detect and communicate errors,MySQL provides a mechanism to access error information using the Mysql_errno () and mysql_error () functions. They can return error codes or error messages about recently called functions, and recently called functions may or may not succeed, so you can tell when the error occurred and what the error was.


Mysql in CAPI The Student Information Management system

  1. first, I was in fedora-8 experiments conducted in the

  2. < P style= "Color:rgb (0,0,0); font-family: ' Calibri ', ' Sans-serif '; font-size:10.5pt; Font-style:normal; Font-weight:normal; margin-top:0cm; Margin-bottom:0pt "> I put mysql required libraries installed in under

  3. Use MySQL in the C API management of individual forms of student information.

Some source code:

void Show (char table_name[])//output table contents {Char str[25];sprintf (str, "SELECT * from%s", table_name); Mysql_real_query (MySQL, STR, (unsigned int) strlen (str)), results = Mysql_store_result (mysql);p rintf ("\ n"), while (field = Mysql_fetch_field ( Results)) {printf ("%s\t\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\t", Record[i]);} printf ("\ n");} Mysql_free_result (results);}

void Insert (char table_name[])  //Add information {char Str1[4][10];char str[30];p rintf ("Please enter first name:"); scanf ("%s", str1[0]); printf ("Enter study Number:"), scanf ("%s", str1[1]);p rintf ("Enter Math score:"), scanf ("%s", str1[2]);p rintf ("Please input language 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, (unsigned int) strlen (str));p rintf (">>> added success!!! \ n ");}

void Delete (char table_name[])  //delete Information {char Delete_name[10];char delete_str[40];p rintf ("Please enter the name of the information to be removed:"); scanf (" %s ", delete_name); sprintf (Delete_str," Delete from%s where name=\ "%s\" ", TABLE_NAME, Delete_name);p rintf (" \n>> >%s\n ", delete_str); Mysql_real_query (MySQL, delete_str, (unsigned int) strlen (delete_str));p rintf (" >>> Delete Success!!! \ n ");}


void Find (char table_name[])//modify information {char Find_str[40];char find_name[10];p rintf ("Please select the name to query:"); scanf ("%s", Find_name) ; sprintf (FIND_STR, "select * from%s where name=\"%s\ "", TABLE_NAME, Find_name);p rintf ("\n>>>%s\n", Find_str) */* Execute SQL query specified as "null-terminated string" */mysql_real_query (MySQL, find_str, (unsigned int) strlen (FIND_STR)); results = Mysql_ Store_result (MySQL);//access to rows previously obtained from the server printf (">>> total%d messages \ n \ nthe", *results); while (field = Mysql_fetch_field ( Results)) {printf ("%s\t\t", Field->name);} printf ("\ n"), while (record = mysql_fetch_row (results))   //can access rows previously obtained from the server {num_fields = Mysql_num_fields (Results ); for (i = 0; i < Num_fields; i++) {printf ("%s\t\t", Record[i]);} printf ("\ n");} printf ("\ n");}

These are just some of the simple features, there are more features please refer to: http://download.csdn.net/detail/u013930856/8377673






Student Information Management for C API in Mysql

Related Article

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.