linux 數據庫編程

wKiom1YbpuWiRQ3yAABm5lAGjro733.jpg

  應用程序對數據庫操做的通常步驟:mysql

    ①創建鏈接
sql

    ②執行查詢數據庫

    ③斷開鏈接
爲方便 C應用程序的開發,MySQL 提供了 c API,它定義了程序與服務器創建通訊鏈接的具體方法。
服務器

1.數據結構session


  數據結構的定義在 mysql-x.x.xxx/include/mysql.h 文件中。數據結構

1.1.鏈接句柄
dom

  要鏈接到 MySQL,須先建立(mysql_init())一個 MYSQL 結構的實例。
socket

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

  /* session-wide random string */
  char          scramble[SCRAMBLE_LENGTH+1];

 /*
   Set if this is the original connection, not a master or a slave we have
   added though mysql_rpl_probe() or mysql_set_master()/ mysql_add_slave()
 */
  my_bool rpl_pivot;
  /*
    Pointers to the master, and the next slave connections, points to
    itself if lone connection.
  */
  struct st_mysql* master, *next_slave;

  struct st_mysql* last_used_slave; /* needed for round-robin slave pick */
 /* needed for send/read/store/use result to work correctly with replication */
  struct st_mysql* last_used_con;

  LIST  *stmts;                     /* list of all statements */
  const struct st_mysql_methods *methods;
  void *thd;
  /*
    Points to boolean flag in MYSQL_RES  or MYSQL_STMT. We set this flag
    from mysql_stmt_close if close had to cancel result set of this object.
  */
  my_bool *unbuffered_fetch_owner;
  /* needed for embedded server - no net buffer to store the 'info' */
  char *info_buffer;
  void *extension;
} MYSQL;


1.2.查詢結果集ide

  從 MySQL 數據庫查詢返回的數據信息,稱爲結果集。在 MySQL 提供的 C函數 接口中對應的就是 MYSQL_RES 結構。
函數

typedef struct st_mysql_res {
  my_ulonglong  row_count;
  MYSQL_FIELD   *fields;
  MYSQL_DATA    *data;
  MYSQL_ROWS    *data_cursor;
  unsigned long *lengths;               /* column lengths of current row */
  MYSQL         *handle;                /* for unbuffered reads */
  const struct st_mysql_methods *methods;
  MYSQL_ROW     row;                    /* If unbuffered read */
  MYSQL_ROW     current_row;            /* buffer to current row */
  MEM_ROOT      field_alloc;
  unsigned int  field_count, current_field;
  my_bool       eof;                    /* Used by mysql_fetch_row */
  /* mysql_stmt_close() had to cancel this result */
  my_bool       unbuffered_fetch_cancelled;
  void *extension;
} MYSQL_RES;


1.3.字段結構

  MYSQL_FIELD 是 MYSQL、MYSQL_RES 的字段結構體。

typedef struct st_mysql_field {
  char *name;                 /* Name of column */
  char *org_name;             /* Original column name, if an alias */
  char *table;                /* Table of column if column was a field */
  char *org_table;            /* Org table name, if table was an alias */
  char *db;                   /* Database for table */
  char *catalog;              /* Catalog for table */
  char *def;                  /* Default value (set by mysql_list_fields) */
  unsigned long length;       /* Width of column (create length) */
  unsigned long max_length;   /* Max width for selected set */
  unsigned int name_length;
  unsigned int org_name_length;
  unsigned int table_length;
  unsigned int org_table_length;
  unsigned int db_length;
  unsigned int catalog_length;
  unsigned int def_length;
  unsigned int flags;         /* Div flags */
  unsigned int decimals;      /* Number of decimals in field */
  unsigned int charsetnr;     /* Character set */
  enum enum_field_types type; /* Type of field. See mysql_com.h for types */
  void *extension;
} MYSQL_FIELD;


1.4.記錄結構

  MYSQL_ROW 表示結果集中的一行記錄。

typedef struct st_mysql_rows {
  struct st_mysql_rows *next;           /* list of rows */
  MYSQL_ROW data;
  unsigned long length;
} MYSQL_ROWS;


2.程序處理過程


  程序處理過程以下:
    ①初始化函數庫     mysql_library_init()
    ②初始化鏈接句柄    mysql_init()
    ③鏈接到數據庫     mysql_real_connect()
    ④查詢
    ⑤關閉數據庫鏈接    mysql_close()
    ⑥結束函數庫調用    mysql_library_end()

2.1.

2.2.

2.3.

2.4.

相關文章
相關標籤/搜索