是否可以从库连接到mysql

Is it possible to connect to mysql from the library so?

本文关键字:连接 mysql 是否      更新时间:2023-10-16

在为外部应用程序编写我的第一个插件时,我遇到了很大的障碍。插件作为DLL文件加载(Linux的SO(尝试连接到数据库,但需要一些其他编译。到处都写有必要使用给定的命令编译应用程序:

gcc exa_7.c -o exa_7 -std=c99  `mysql_config --cflags --libs'

该命令是最正确的,但我无法组合到属性:-c -fPIC

当我尝试在不传递 MYSQL 所需的属性的情况下触发代码时,它会导致我很多错误:

dllmain.c:46:5: error: unknown type name ‘MYSQL’; did you mean ‘MYSQL_XID’?
     MYSQL      *MySQLConRet;
     ^~~~~
     MYSQL_XID
dllmain.c:47:5: error: unknown type name ‘MYSQL’; did you mean ‘MYSQL_XID’?
     MYSQL      *MySQLConnection = NULL;
     ^~~~~
     MYSQL_XID
dllmain.c:74:5: error: unknown type name ‘MYSQL_RES’; did you mean ‘MYSQL_XID’?
     MYSQL_RES      *mysqlResult = NULL;
     ^~~~~~~~~
     MYSQL_XID
dllmain.c:90:6: error: unknown type name ‘MYSQL_ROW’; did you mean ‘MYSQL_XID’?
      MYSQL_ROW       mysqlRow;
      ^~~~~~~~~
      MYSQL_XID
dllmain.c:91:6: error: unknown type name ‘MYSQL_FIELD’; did you mean ‘MYSQL_XID’?
      MYSQL_FIELD    *mysqlFields;
      ^~~~~~~~~~~
      MYSQL_XID

我的清单包括:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <mysql/mysql.h>
#include <mysql/my_global.h>
#include <mysql/my_config.h>
#include <sys/socket.h> 
#include <arpa/inet.h>
#include "ts3_functions.h"
#include "plugin.h"
#include <pthread.h>
//(more includes, but not important)

您知道如何解决此问题吗?

//编辑:删除#include <mysql/mysql.h>的评论

现在我用命令编译了它:gcc -pthread -c -fPIC -o test.o dllmain.c

编译运行正常(没有任何错误(,但在运行时程序收到消息:undefined symbol: mysql_init

也许问题出在代码中?

MYSQL      *MySQLConRet;
MYSQL      *MySQLConnection = NULL;
MySQLConnection = mysql_init( NULL );
MySQLConRet = mysql_real_connect( MySQLConnection,
                                  hostName, 
                                  userId, 
                                  password, 
                                  DB, 
                                  0, 
                                  NULL,
                                  0 );
if ( MySQLConRet == NULL ) {
    printf(mysql_error(MySQLConnection) );
    exit(1);
}

您尚未包含<mysql/mysql.h> .取消注释该行。此文件是使用 MySQL API 所必需的。 <mysql/my_global.h>是一个内部头文件,用于包含公共系统头文件和定义实用程序宏 - 它不能替代<mysql/mysql.h>,根本不应该直接包含。