我正在尝试将C程序连接到Mysql。
我在主要部分有这个代码(我已经包含了mysql.h头文件)
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);
由于
答案 0 :(得分:0)
您应该创建公共头文件并在test.c中包含此文件:示例
functionA