MySQL C API

來源:互聯網
上載者:User

1.標頭檔,API包含在mysqlclient庫中

#include <mysql.h>

在編譯時間,要加上-lmysqlclient

2.建立變數MYSQL mysql

MYSQL結構代表1個資料庫連接控制代碼,幾乎所有的MySQL函數都會使用到,其結構體如下:

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.初始化mysql變數,調用mysql_init(&mysql),並調用mysql_real_connect()串連資料庫,其函數原型如下:

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.構建sql語句並進行查詢:mysql_real_query,函數原型:

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

5.聲明一個MYSQL_RES類型的變數:MYSQL_RES res,結構體如下:

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;
然後將查詢結果儲存到MYSQL_RES中:
res = mysql_store_result(&mysql);
函數原型:

MYSQL_RES *mysql_store_result(MYSQL *mysql)

如果查詢未返回結果集,mysql_store_result()將返回Null指標(例如,如果查詢是INSERT語句)。

如果讀取結果集失敗,mysql_store_result()還會返回Null指標。通過檢查mysql_error()是否返回非Null 字元串,mysql_errno()是否返回非0值,或mysql_field_count()是否返回0,可以檢查是否出現了錯誤。

如果未返回行,將返回空的結果集。(空結果集設定不同於作為傳回值的null 指標)。

6.一旦調用了mysql_store_result()並獲得了不是Null指標的結果,調用mysql_num_rows()來找出結果集中的行數,調用mysql_fetch_row()來擷取結果集中的行,或調用mysql_row_seek()和mysql_row_tell()來擷取或設定結果集中的當前行位置

7.調用mysql_free_result()釋放結果集

8. 通過調用mysql_close(),關閉與MySQL伺服器的串連

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.