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)
- const char *host 是地址,null的时候默认是本机
- unsigned int port 是端口号,默认是3306。有多个版本mysql的话需要改端口号
- unix_socket:套接字,相当于是一个文件,目的将文件里面的数据与网卡交互。默认是0或者NULL
- client flag:标志位。使用时可以去查。其中CLIENT_OPTIONAL_RESULTSET_METADATA包含表信息(包含字段名,类型),如果不需要这些数据,可以把它关掉
- 执行成功时,他会返回一个FALSE!!
mysql_options(设置项)
-
超时设定
int to = 3; int re = mysql_options(&mysql, MYSQL_OPT_CONNECT_TIMEOUT, &to); //mysql_options可能执行失败,失败返回一个非0的值 if (re != 0) { cout << "mysql_options failed!" << mysql_error(&mysql) << endl; }
-
自动重连
//自动重连 int recon = 1; re = mysql_options(&mysql, MYSQL_OPT_RECONNECT, &recon); if (re != 0) { cout << "mysql_options failed!" << mysql_error(&mysql) << endl; }
还有其他的关键字可以参考它的定义,这里不一一写了。