MySQL C API

Source: Internet
Author: User

1. header file. The API is included in the mysqlclient library.

# Include <mysql. h>

-Lmysqlclient must be added during compilation.

2. Create a variableMYSQLMysql

The MYSQL structure represents a database connection handle, which is used by almost all MySQL functions. Its structure is as follows:

typedef struct st_mysql {
  NET           net;            /* Communication parameters */
  gptr          connector_fd;   /* ConnectorFd for SSL */
  char          *host,*user,*passwd,*unix_socket,
                *server_version,*host_info,*info,*db;
  unsigned int  port,client_flag,server_capabilities;
  unsigned int  protocol_version;
  unsigned int  field_count;
  unsigned int  server_status;
  unsigned long thread_id;      /* Id for connection in server */
  my_ulonglong affected_rows;
  my_ulonglong insert_id;       /* id if insert on table with NEXTNR */
  my_ulonglong extra_info;              /* Used by mysqlshow */
  unsigned long packet_length;
  enum mysql_status status;
  MYSQL_FIELD   *fields;
  MEM_ROOT      field_alloc;
  my_bool       free_me;        /* If free in mysql_close */
  my_bool       reconnect;      /* set to 1 if automatic reconnect */
  struct st_mysql_options options;
  char          scramble_buff[9];
  struct charset_info_st *charset;
  unsigned int  server_language;
} MYSQL;

3. initialize the mysql variable, call mysql_init (& mysql), and call mysql_real_connect () to connect to the database. The function prototype is as follows:

MYSQL * 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 client_flag)

4. Construct and query SQL statements:Mysql_real_query, function prototype:

Int mysql_real_query (MYSQL * mysql, const char * query, unsigned long length)

5. Declare a variable of the MYSQL_RES type: MYSQL_RES. The struct is as follows:

typedef struct st_mysql_res {
  my_ulonglong row_count;
  unsigned int  field_count, current_field;
  MYSQL_FIELD   *fields;
  MYSQL_DATA    *data;
  MYSQL_ROWS    *data_cursor;
  MEM_ROOT      field_alloc;
  MYSQL_ROW     row;            /* If unbuffered read */
  MYSQL_ROW     current_row;    /* buffer to current row */
  unsigned long *lengths;       /* column lengths of current row */
  MYSQL         *handle;        /* for unbuffered reads */
  my_bool       eof;            /* Used my mysql_fetch_row */
} MYSQL_RES;
Then store the query results in MYSQL_RES:
res = mysql_store_result(&mysql);
Function prototype:

MYSQL_RES * mysql_store_result (MYSQL * mysql)

If no result set is returned for the query, mysql_store_result () returns a Null pointer (for example, if the query is an INSERT Statement ).

If the read result set fails, mysql_store_result () returns a Null pointer. By checking whether mysql_error () returns a non-empty string, mysql_errno () returns a non-0 value, or mysql_field_count () returns 0, you can check whether an error has occurred.

If no row is returned, an empty result set is returned. (Null result set settings are different from null pointers used as return values ).

6. once mysql_store_result () is called and the result is not a Null pointer, call mysql_num_rows () to find the number of rows in the result set and call mysql_fetch_row () to obtain the rows in the result set, you can also call mysql_row_seek () and mysql_row_tell () to obtain or set the current row location in the result set.

7. Call mysql_free_result () to release the result set.

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

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.