int mysql_query(MYSQL *mysql, const char *stmt_str)
功能:
html
MYSQL *mysql_conn; mysql_conn = mysql_init( NULL );
【關於mysql_connect()】 mysql
mysql_connect() 屬於老 API,已經被 mysql_real_connect() 所取代。#ifdef USE_OLD_FUNCTIONS MYSQL * STDCALL mysql_connect(MYSQL *mysql,const char *host, const char *user, const char *passwd) { MYSQL *res; mysql=mysql_init(mysql); /* Make it thread safe */ { DBUG_ENTER("mysql_connect"); if (!(res=mysql_real_connect(mysql,host,user,passwd,NullS,0,NullS,0))) { if (mysql->free_me) my_free(mysql); } mysql->reconnect= 1; // 直接設置了重連 DBUG_RETURN(res); } } #endif
調用 mysql_connect() 與 mysql_real_connect() 時輸入參數意義相同,差異在於 mysql_connect() 中鏈接句柄能夠爲 NULL 。在這種狀況下,C API 將自動爲鏈接結構分配內存,並在調用 mysql_close() 時釋放分配的內存。該方法的缺點是,若是鏈接失敗,你沒法檢索錯誤消息。要想從 mysql_errno() 或 mysql_error() 得到錯誤消息,必須提供有效的 MYSQL 指針。而 mysql_real_connect() 中的鏈接句柄必須是已初始化過的 MYSQL 結構。 sql
【關於「重連」的設置】/* By default we don't reconnect because it could silently corrupt data (after reconnection you potentially lose table locks, user variables, session variables (transactions but they are specifically dealt with in mysql_reconnect()). This is a change: < 5.0.3 mysql->reconnect was set to 1 by default. How this change impacts existing apps: - existing apps which relyed on the default will see a behaviour change; they will have to set reconnect=1 after mysql_real_connect(). - existing apps which explicitely asked for reconnection (the only way they could do it was by setting mysql.reconnect to 1 after mysql_real_connect()) will not see a behaviour change. - existing apps which explicitely asked for no reconnection (mysql.reconnect=0) will not see a behaviour change. */ mysql->reconnect= 0;
意思大概以下:默認狀況下,咱們不執行重連動做,由於可能會存在不易覺察的數據損壞(在重連後,有可能發生的狀況包括,丟失表鎖、用戶變量、會話變量等(事務固然也會終止,可是會在 mysql_reconnect() 時獲得正確處理))。 數據庫
在 MySQL 5.0.3 版本以前,mysql->reconnect 默認被設置爲 1 。unsigned int timeout = 10; mysql_options( mysql_conn, MYSQL_OPT_CONNECT_TIMEOUT, (char *)&timeout )
【關於「字符集」的設置】 服務器
目前存在三種方式設置字符集:/* mysql_set_character_set function sends SET NAMES cs_name to the server (which changes character_set_client, character_set_result and character_set_connection) and updates mysql->charset so other functions like mysql_real_escape will work correctly. */
mysql_set_character_set() 會向服務器發送 "SET NAMES xxx" 命令來設置字符集信息(該設置會改變 character_set_client、character_set_result 和 character_set_connection 的值),並會更新 mysql->charset 的值,進而對 mysql_real_escape() 等函數產生影響。 session
該函數內部會int mysql_library_init(int argc, char **argv, char **groups)
對於常規的客戶端應用程序來講,一般以 mysql_library_init(0, NULL, NULL) 進行調用。 app
#include <mysql.h> #include <stdlib.h> int main(void) { if (mysql_library_init(0, NULL, NULL)) { fprintf(stderr, "could not initialize MySQL library\n"); exit(1); } /* Use any MySQL API functions here */ mysql_library_end(); return EXIT_SUCCESS; }
The groups argument is an array of strings that indicate the groups in option files from which to read options. For convenience, if the groups argument itself is NULL, the [server] and [embedded] groups are used by default. 函數
int mysql_next_result(MYSQL *mysql)
若是存在多個查詢結果,mysql_next_result() 將讀取下一個查詢結果,並將狀態返回給應用程序。若是前面的查詢返回告終果集,必須爲其調用 mysql_free_result()。 測試
調用 mysql_next_result() 後,鏈接狀態就像你已爲下一查詢調用了 mysql_real_query() 或 mysql_query() 同樣。這意味着你能調用 mysql_store_result()、mysql_warning_count()、mysql_affected_rows() 等等。若是 mysql_next_result() 返回錯誤,將不執行任何其餘語句,也不會獲取任何更多的結果。 this
返回值 |
描述 |
0 | 成功並有多個結果 |
-1 | 成功但沒有多個結果 |
>0 | 出錯 |
/* Connect to server with option CLIENT_MULTI_STATEMENTS */ mysql_real_connect(..., CLIENT_MULTI_STATEMENTS); /* Now execute multiple queries */ mysql_query(mysql,"DROP TABLE IF EXISTS test_table;\ CREATE TABLE test_table(id INT);\ INSERT INTO test_table VALUES(10);\ UPDATE test_table SET id=20 WHERE id=10;\ SELECT * FROM test_table;\ DROP TABLE test_table"); do { /* Process all results */ ... printf("total affected rows: %lld", mysql_affected_rows(mysql)); ... if (!(result= mysql_store_result(mysql))) { printf(stderr, "Got fatal error processing query\n"); exit(1); } process_result_set(result); /* client function */ mysql_free_result(result); } while (!mysql_next_result(mysql));
多語句功能可與 mysql_query() 或 mysql_real_query() 一塊兒使用。它不能與預處理語句接口一塊兒使用。按照定義,預處理語句僅能與包含單個語句的字符串一塊兒使用。