int mysql_query(MYSQL *mysql, const char *stmt_str)
功能:
MYSQL *mysql_conn; mysql_conn = mysql_init( NULL );
【关于mysql_connect()】
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 结构。
【关于“重连”的设置】/* 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() 等函数产生影响。
该函数内部会int mysql_library_init(int argc, char **argv, char **groups)
对于常规的客户端应用程序来说,通常以 mysql_library_init(0, NULL, NULL) 进行调用。
#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() 返回错误,将不执行任何其他语句,也不会获取任何更多的结果。
返回值 |
描述 |
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() 一起使用。它不能与预处理语句接口一起使用。按照定义,预处理语句仅能与包含单个语句的字符串一起使用。