mysql - 循环 mysql_real_connect 或者什么

标签 mysql c

考虑以下 C 代码:

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

将连接函数的主体复制到 main 中并删除连接函数可以解决问题。但这不是解决方案。

最佳答案

重命名方法

void connect(void)

void connect_to_database(void)

帮我解决了这个问题

关于mysql - 循环 mysql_real_connect 或者什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6188799/

相关文章:

php - 从 PHP 插入 MySQL

c - vm_insert_page() 和 remap_pfn_range() 有什么区别?

c - 标题的相对路径(例如 "../include/header.h")有什么好处?

c - MSVC 和优化常量表达式

C - 如何从文件中读取某些行? (不是所有行或整个文件,而是某些行)

php - 自动将 SQL 表内容发送到电子邮件地址

c# - 读取MySql binlog

php - 提交 php 表单时出现 "You have an error in your SQL syntax"错误

c - 将字符串写入十六进制转换器

php - 如何使用 PHP 检查数据是否已存在于 MySQL 中