MySQL's C language API interface _c language

Source: Internet
Author: User

1, the first of course is to connect the database, the function prototype is as follows:

MySQL * stdcall mysql_real_connect (MySQL *mysql, const char *host,
const char *user,
const char *PASSWD,
cons T Char *db,
unsigned int port,
const char *unix_socket,
unsigned long clientflag);

The first parameter MySQL is a very important variable in the C API, the memory inside is very rich, have port,dbname,charset and so on connection basic parameter. It also contains a struct variable called st_mysql_methods, which holds many function pointers that will be invoked in various data operations after the successful database connection. Mysql_real_connect function of each parameter, basically all is as implies meaning.

2, after the successful connection of the database can execute SQL statements using mysql_query
int stdcall mysql_query (MySQL *mysql, const char *q);

The first parameter is described above, and the second parameter is the SQL statement to execute.

This function is generally two steps:

(1) Send SQL statements, in fact, a socket to send SQL statements, plus MySQL fixed the protocol header. Lazy to see the source code, grabbed the next package, as follows:
0000 0373 6c (5f ...).., select App_, ...
0010 6e 6d GHz, 6f 6d name from app
The red part is the protocol, the front two bits is actually the length of the package. No specific agreement has been studied.

(2) Then the result is accepted, and here will call the Read_query_result function pointer in the st_mysql_methods in the MySQL variable

3. Get the result

After the execution of SQL, if it is a query statement, we will of course read the data, if Update,insert and other statements, then see if the operation is successful or not. Let's look at how to get the results of the query: If Mysql_query returns successfully, then we'll read the result by mysql_store_result this function. The prototype is as follows:

Mysql_res * stdcall mysql_store_result (MySQL *mysql);

The function invokes the Read_rows function pointer in the st_mysql_methods in the MySQL variable to get the result of the query. At the same time, the function returns mysql_res such a variable, which is used primarily to hold the results of the query. At the same time the function malloc a piece of memory space to store the data from the query, so we have to remember the free (results), otherwise it will certainly cause memory leaks. After the completion of the Mysql_store_result, in fact, the data are already in the Mysql_res variable, the following API is basically read the data in the Mysql_res. For example, the Mysql_fetch_row function is a line that reads the results of a query. The function prototype is as follows

Mysql_row stdcall mysql_fetch_row (mysql_res *result);

It returns a Mysql_row variable, mysql_row is actually char * *. Just use it as a two-dimensional array. There are a lot of APIs, no longer introduced, most of the information is in the mysql_res MySQL two structures. MySQL can refer to the official website: http://dev.mysql.com/doc/refman/5.1/en/c.html suddenly found that the official website information is good comprehensive, seemingly better than any book.

Here's an example:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <mysql/mysql.h> #define MAX
  _column_len int main (int argc, char *argv[]) {MYSQL my_connection;
  Mysql_res *result;
  Mysql_row Sql_row;
  Mysql_field *FD;
  Char Column[max_column_len][max_column_len];
  int res;
  Mysql_init (&my_connection);
    if (Mysql_real_connect (&my_connection, "127.0.0.1", "User", "password", "Data Name", 3306,null,0)) {perror ("connect"); Res=mysql_query (&my_connection, "SELECT * from App")//query if (!res) {Result=mysql_store_result (&my_co
        nnection);//Save the queried data to result if (result) {int i,j;
        printf ("The result number was%lu\n", (unsigned long) mysql_num_rows (result));
          for [I=0;fd=mysql_fetch_field (result); i++)//Get column name {bzero (column[i],sizeof (Column[i]));
        strcpy (Column[i],fd->name);
        } j=mysql_num_fields (Result); for (i=0;i<j;i++) {printf ("%s\t", COlumn[i]);
        printf ("\ n"); while (Sql_row=mysql_fetch_row)//Gets the specific data {for (i=0;i<j;i++) {printf ("%s
          \ t ", sql_row[i]);
        printf ("\ n");
    }} else {perror ("select");
  } else {perror ("Connect:error"); } mysql_free_result (Mysql_res *result);/Free result resource mysql_close (&my_connection);//Disconnect}

The example above is to look up data from a table and output it. If you want to insert or update, you only need to modify the specific SQL. The specific operation is done by mysql_query this function. Now for the compile method, here we need. h and. So library. We can download connector/c in http://dev.mysql.com/downloads/connector/c/6.0.html. The easy way to do this is to copy the contents included in the/usr/include/mysql/below, so that you don't need to add-I when you compile it, and then copy the things below the Lib/usr/lib/down.

GCC specific compilation method: GCC ***.c-o * * * *-lmysqlclient

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.