mysql - C Mysql 连接处理程序

标签 mysql c

我正在尝试将 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/

相关文章:

php - 将外国安全主体翻译/解码为可读名称

php - 从 MySQL 表中获取最近的日期

mysql - 类似 IN 的 SQL 查询

计算c数组中特定范围的频率随机数

c - SDL 中的文本输入框

MySql 无法从 LAN 连接

MySQL pid 结束(无法启动 mysql)

c - 在 C 中无法正确调试(Eclipse Mars)

c - Read() 在读取实际数据之前先读取垃圾数据

c - 与 HTTP 1.0 相比,HTTP 1.1 延迟 20 秒