考慮下面的C代碼:循環mysql_real_connect還是什麼
int main (int argc, char *argv[]) {
MYSQL *sql_handle;
fprintf(stdout,"initializing handle..\n");
sql_handle = mysql_init(sql_handle);
fprintf(stdout,"connecting to database..\n");
mysql_real_connect(sql_handle,NULL,NULL,
NULL,"test",0,NULL,0);
fprintf(stdout,"connection established\n");
mysql_close(sql_handle);
}
這將產生以下輸出
...
initializing handle..
connecting to database..
initializing handle..
connecting to database..
initializing handle..
connecting to database..
initializing handle..
connecting to database..
connection established
Error: Can't create UNIX socket (24)
的real_connect功能似乎有問題。守護進程確實運行。很久以前,因爲我習慣了c,所以這可能是一個愚蠢的問題。
[更新] 這裏的完整代碼
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #if defined __WIN32__ || _MSC_VER
5 #include "my_global.h"
6 #include "mysql.h"
7 #else
8 #include <mysql.h>
9 #endif
10
11 /* prototypes */
12 void connect(void);
13
14 /* sql handle */
15 MYSQL *sql_handle;
16
17 int main (int argc, char *argv[]) {
18 fprintf(stdout,"main..\n");
19 connect();
20 return EXIT_SUCCESS;
21
22 }
23 void connect(void){
24 fprintf(stdout,"initializing database handle..\n");
25 sql_handle = mysql_init(NULL);
26
27 fprintf(stdout,"connecting to database..\n");
28 mysql_real_connect(sql_handle,NULL,NULL,NULL,NULL,0,NULL,0);
29
30 fprintf(stdout,"closing connection..\n");
31 mysql_close(sql_handle);
32 }
此代碼產生輸出:
...
connecting to database..
initializing database handle..
connecting to database..
initializing database handle..
connecting to database..
closing connection..
[1] 12914 segmentation fault (core dumped) ./db
複製連接功能的身體進入主以及去除所述連接 - 功能解決了這個問題。但這不是一個解決方案。
[Hmmmmmmmm](http://dev.mysql.com/doc/refman/5.0/en/mysql-init.html)...嘗試'sql_handle = mysql_init(NULL);' – pmg 2011-05-31 14:10:15
我無法看到這可能會產生一個循環。 – 2011-05-31 14:33:51
我很確定,這不是真正的代碼。即使'mysql_real_connect'在內部做了一些循環(我認爲它沒有這樣做),連接到數據庫被寫了好幾次。 – 2011-05-31 14:35:11