我正在尝试将 C 程序连接到 Mysql。
我在主要部分中有此代码(我已包含 mysql.h header )
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
int main (int argc, char *argv[])
{
conn = my_init();
mysqll_real_connect(conn,"localhost","user","pass","database",0,NULL,0);
我在 main 之外有一个调用该 Connection 的函数
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_close(conn);
谢谢
最佳答案
您应该制作通用头文件并将此文件包含在 test.c 中:示例
#ifndef _MAIN_H
#define _MAIN_H 1
#include <mysql/mysql.h>
extern MYSQL *conn;
extern MYSQL_RES *res;
extern MYSQL_ROW row;
#endif
关于mysql - C Mysql 连接处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36557889/