In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
The following content mainly brings you mysql programming introduction to query execution of C API processing methods, the knowledge here is slightly different from books, are professional and technical personnel in the process of contact with users, summed up, has a certain experience sharing value, hope to bring help to the majority of readers.
C API processing for multi-query execution
supports the execution of multiple statements specified in a single string. To use this feature with a given connection, you must assign the CLIENT_MULTI_STATEMENTS option in the flag parameter to mysql_real_connect () when you open the connection. You can also set it for an existing connection by calling mysql_set_server_option (MYSQL_OPTION_MULTI_STATEMENTS_ON)
Just a few points for more queries.
The CLIENT_MULTI_STATEMENTS option specifies mysql_next_result
Int mysql_next_result (MYSQL * mysql)
Description
If there is more than one query result, mysql_next_result () reads the next query result and returns the status to the application.
If the previous query returns a result set, you must call mysql_free_result () for it.
When mysql_next_result () is called, the connection state is the same as if you had called mysql_real_query () or mysql_query () for the next query. This means you can call mysql_store_result (), mysql_warning_count (), mysql_affected_rows (), and so on.
If mysql_next_result () returns an error, no other statements will be executed and no more results will be obtained
Return value
Description
0 was successful and had multiple results.
-1
It was successful but there were no multiple results.
0 error
# include
# include
# include
# include
Int process_result_set (MYSQL mysql, MYSQL_RES result)
{
Int ret = 0, I = 0
Unsigned int num = 0
If (mysql==NULL | | result==NULL) {ret = 0; printf ("func process_result_set () err:%d, check if (mysql==NULL | | result==NULL)\ n", ret); return ret;} num = mysql_field_count (mysql); / / find the header MYSQL_FIELD * fields = mysql_fetch_fields (result); for (I = 0; I 0) printf ("Could not execute statement\ n");} while (status = 0); mysql_close (connect) Printf ("hello...\ n"); return ret
}
Binary protocols allow you to send and accept date and time values (DATE, TIME, DATETIME, and TIMESTAMP) using MYSQL_TIME structures.
To send temporary data values, use mysql_stmt_prepare () to create a preprocessing statement. Then, before calling mysql_stmt_execute () to execute the statement, you can set each temporary parameter using the following steps:
In the MYSQL_BIND structure associated with the data value, set the buffer_type member to the appropriate type, which indicates the type of temporary value to be sent. For DATE, TIME, DATETIME, or timestamp values, set buffer_type to MYSQL_TYPE_DATE, MYSQL_TYPE_TIME, MYSQL_TYPE_DATETIME, or MYSQL_TYPE_TIMESTAMP, respectively.
Sets the buffer member of the MYSQL_BIND structure to the address of the MYSQL_TIME structure used to pass temporary values.
Populate the members of the MYSQL_TIME structure to match the type of temporary branch you intend to pass.
Use mysql_stmt_bind_param () to bind parameter data to a statement. You can then call mysql_stmt_execute ().
To retrieve temporary values, you can take a similar step, but set the buffer_type member to the type of value you intend to accept and the buffer member to the address of the MYSQL_TIME structure in which the return value should be placed. After calling mysql_stmt_execute (), and before getting the result, use mysql_bind_results () to bind the buffer to the statement.
Please look at the code:
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include
Int main ()
{
Int ret = 0, status = 0
MYSQL mysql
MYSQL_RES result
Mysql = mysql_init (NULL); mysql = mysql_real_connect (mysql, "localhost", "root", "123456", "mydb2", 0, NULL, CLIENT_MULTI_STATEMENTS); if (mysql = = NULL) {ret = mysql_errno (mysql); printf ("% s", mysql_error (mysql)); printf ("func mysql_real_connect () err:% d\ n", ret); return ret;} else {printf ("ok.\ n") } MYSQL_TIME ts;MYSQL_BIND bind [3]; MYSQL_STMT * stmt; / / Note: / / created table statement / / create table test_table (date_field date, time_field time, timestamp_field timestamp); char query [1024] = "INSERT INTO test_table (date_field, time_field, timestamp_field) VALUES (?,?)"; stmt = mysql_stmt_init (mysql) If (! stmt) {fprintf (stderr, "mysql_stmt_init (), out of memory\ n"); exit (0);} if (mysql_stmt_prepare (stmt, query, strlen (query) / / add sql language with placeholder {fprintf (stderr, "\ n mysql_stmt_prepare (), INSERT failed") to the environment handle; fprintf (stderr, "\ n% s", mysql_stmt_error (stmt)); exit (0) } / * set up input buffers for all 3 parameters * / bind [0] .buffer _ type= MYSQL_TYPE_DATE; / / sets the binding variable attribute bind [0] .buffer = (char *) & ts;bind [0] .is _ null= 0x bind [0] .bind = bind [2] = bind [0]; /... mysql_stmt_bind_param (stmt, bind); / * supply the data to be sent in the ts structure * / ts.year= 2002 ts.month = 02 Ts.day= 03posits.hour.hour= 10posits.minutes45witts.second20 mysqlinherstmtexecute (stmt); / / Close the statement / / if (mysql_stmt_close (stmt)) {fprintf (stderr, "failed while closing the statement\ n"); fprintf (stderr, "% s\ n", mysql_stmt_error (stmt)); exit (0);} mysql_close (mysql)
For the above C API processing methods for query execution on the introduction to mysql programming, if you have more information to know, you can continue to pay attention to the innovation of our industry. If you need professional answers, you can contact the pre-sale and after-sale ones on the official website. I hope this article can bring you some knowledge updates.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.