MySQL C language API interface

  • 2020-04-02 03:16:28
  • OfStack

1. First of all, connect to the database. The function prototype 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 clientflag);

The first parameter MYSQL is a very important variable in C API, which has a lot of memory, including port,dbname,charset and other basic connection parameters. It also contains a struct variable called st_mysql_methods, which holds a number of function Pointers that will be called during various data operations after a successful database connection. Mysql_real_connect function of each parameter, is basically the name.

After a successful connection to the database, you can execute the SQL statement 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 has two steps:

(1) send SQL statement, in fact, a socket to send SQL statement, plus mysql fixed protocol header. Lazy to look at the source, caught the next package, as follows:
0000 19 0000 00 0373 65 6c 65 63 74 20 61 70 70 5f... The select app_
0010 6e 661 6d 65 20 66 72 6f 6d 20 61 70 70 name from app
The red part is the protocol. The first two bits are the length of the package. Specific agreements have not been studied.

(2) then accept the result, which calls the read_query_result function pointer in the MYSQL variable st_mysql_methods

3. Get results

After the SQL execution, if it is a query statement, of course, we also want to read the data, if the update statement, insert, and so on, then see whether the operation is successful or not. Let's look at how to get the query result: if mysql_query returns success, we read the result through the mysql_store_result function. The prototype is as follows:

MYSQL_RES * STDCALL mysql_store_result(MYSQL * MYSQL);

This function calls the read_rows function pointer in the st_mysql_methods MYSQL variable to get the result of the query. This function also returns a variable called MYSQL_RES, which is used to store the results of the query. At the same time, this function malloc has a memory space to store the data from the query, so we must remember the free(result), or it will definitely cause memory leak. After executing mysql_store_result, the data is already in the MYSQL_RES variable. The following API is basically to read the data in MYSQL_RES. For example, mysql_fetch_row is a function that reads the result of a query. The function prototype is as follows

MYSQL_ROW STDCALL mysql_fetch_row (MYSQL_RES * result);

It returns a variable called MYSQL_ROW, which is essentially a char **. There are a lot of apis, and I won't go through them all, but most of the information is in the MYSQL_RES MYSQL structure. Specific can refer to the mysql's official website: http://dev.mysql.com/doc/refman/5.1/en/c.html suddenly found good official website information 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  32
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"," The user "," password "," The name of the data ",3306,NULL,0))
  {
    perror("connect");
    res=mysql_query(&my_connection,"select * from app");//The query
    if(!res)
    {
      result=mysql_store_result(&my_connection);// save The query To data to result
      if(result)
      {
        int i,j;
        printf("the result number is %lun ",(unsigned long)mysql_num_rows(result));
        for(i=0;fd=mysql_fetch_field(result);i++)//To get the 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("%st",column[i]);
        }
        printf("n");
        while(sql_row=mysql_fetch_row(result))//Get specific data
        {
          for(i=0;i<j;i++)
          {
            printf("%st",sql_row[i]);
          }
          printf("n");
        }
        
      }
    }
    else
    {
      perror("select");
    }
  }
  else
  {
    perror("connect:error");
  }
  mysql_free_result(MYSQL_RES *result);//Release resulting resources
  mysql_close(&my_connection);//disconnect

}

The above example is to look up data from a table and output it. If you want to insert or update, you just need to modify the specific SQL. The specific operation is done through the function mysql_query. Now let's talk about how to compile, where we need the.h and.so libraries. We can download the Connector/C on http://dev.mysql.com/downloads/connector/c/6.0.html. The easy way to do this is to copy the included stuff under /usr/includer/mysql/so you don't have to add minus I when you compile, and then copy /usr/lib/ under the lib stuff.

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


Related articles: