C Mysql连接处理程序
我正在尝试将一个C程序连接到Mysql 我在主要部分有这段代码(我已经包含了mysql.h头) 我在main之外有一个函数调用这个连接C Mysql连接处理程序,mysql,c,Mysql,C,我正在尝试将一个C程序连接到Mysql 我在主要部分有这段代码(我已经包含了mysql.h头) 我在main之外有一个函数调用这个连接 mysql_query(conn,"show tables"); res = mysql_store_result(conn); while ( row = mysql_fetch_row(res) ) { fprintf (stdout,"%s\n", row[0]); } fprintf (st
mysql_query(conn,"show tables");
res = mysql_store_result(conn);
while ( row = mysql_fetch_row(res) )
{
fprintf (stdout,"%s\n", row[0]);
}
fprintf (stdout,"\n%lu rows affected\n", (unsigned long)
mysql_num_rows(res));
问题是当我以这种方式编译代码时
gcc -D__DEBUG__=0 -Wall test.c -lmysql -o test $(mysql_config --libs --include --cflags)
我收到这些信息
test.c:162:14: error: ‘conn’ undeclared (first use in this function)
test.c:162:14: note: each undeclared identifier is reported only once
for each function it appears in
test.c:163:6: error: ‘res’ undeclared (first use in this function)
test.c:164:10: error: ‘row’ undeclared (first use in this function)
那么,我能做的就是只有一个连接并在另一个函数中重用它吗
在主出口我做了一个mysql_关闭(康涅狄格州)
谢谢您应该创建公共头文件并将此文件包含在测试中。c:示例
#ifndef _MAIN_H
#define _MAIN_H 1
#include <mysql/mysql.h>
extern MYSQL *conn;
extern MYSQL_RES *res;
extern MYSQL_ROW row;
#endif
\ifndef\u MAIN\u H
#定义_MAIN_H 1
#包括
外部MYSQL*conn;
外部MYSQL_RES*RES;
外部MYSQL_行;
#恩迪夫
变量conn、res和row的定义是否正确?它们是全球性的吗?
#ifndef _MAIN_H
#define _MAIN_H 1
#include <mysql/mysql.h>
extern MYSQL *conn;
extern MYSQL_RES *res;
extern MYSQL_ROW row;
#endif