我們
在用mysql的c API時,用到最多的就是MYSQL結構了。毫無疑問這個結構是mysql c/c++編程的核心了。
以下是該結構的定義:
typedef struct st_mysql {
NETnet; /* 網路連接參數 */
gptrconnector_fd; /* SSL 串連控制代碼 */
char*host,*user,*passwd,*unix_socket,*server_version,*host_info,*info,*db; // 串連參數
unsigned intport,client_flag,server_capabilities; //串連參數
unsigned intprotocol_version;
unsigned intfield_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;// the field info
MEM_ROOTfield_alloc;// the field memory
my_boolfree_me;/* If free in mysql_close */
my_boolreconnect;/* set to 1 if automatic reconnect */
struct st_mysql_options options;//option
char scramble_buff[9];// ???
struct charset_info_st *charset;// charset info.
unsigned int server_language;// server language/
} MYSQL;
以下是net的定義,這個結構被用來進行網路讀寫:
typedef struct st_net {
Vio* vio;
my_socket fd;/* For Perl DBI/dbd */
int fcntl;
unsigned char *buff,*buff_end,*write_pos,*read_pos;
char last_error[MYSQL_ERRMSG_SIZE];
unsigned int last_errno,max_packet,timeout,pkt_nr;
unsigned char error;
my_bool return_errno,compress;
my_bool no_send_ok; /* needed if we are doing several
queries in one command ( as in LOAD TABLE ... FROM MASTER ),
and do not want to confuse the client with OK at the wrong time
*/
unsigned long remain_in_buf,length, buf_length, where_b;
unsigned int *return_status;
unsigned char reading_or_writing;
char save_char;
} NET;
我們在進行mysql串連以及查詢時,MYSQL這個結構被用做傳送資訊的載體。
需要經常用到MYSQL結構的cAPI:
mysql_connect();
mysql_real_connect();
mysql_query();
mysql_store_result();
mysql_use_result();
mysql_error()
大家有興趣可以看看這幾個函數的實現,會有意想不到的收穫!