c++ - 通过 C++ 连接到 mySQL

标签 c++ mysql linux

我已经使用

安装了 mySQL

sudo apt-get install mySQL-server

然后我使用

安装了 libmysqlclient15-dev

sudo apt-get install libmysqlclient15-dev

此外,我已经使用

安装了 libmysqlc++-dev

sudo apt-get install libmysqlc++-dev

在所有这一切之后,我尝试使用

运行以下代码

g++ test.c -I/usr/include/mysql -I/usr/include/mysql++

#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
#include <mysql++.h>


// just going to input the general details and not the port numbers
struct connection_details
{
    char *server;
    char *user;
    char *password;
    char *database;
};

MYSQL* mysql_connection_setup(struct connection_details mysql_details)
{
     // first of all create a mysql instance and initialize the variables within
    MYSQL *connection = mysql_init(NULL);

    // connect to the database with the details attached.
    if (!mysql_real_connect(connection,mysql_details.server, mysql_details.user, mysql_details.password, mysql_details.database, 0, NULL, 0)) {
      printf("Conection error : %s\n", mysql_error(connection));
      exit(1);
    }
    return connection;
}

MYSQL_RES* mysql_perform_query(MYSQL *connection, char *sql_query)
{
   // send the query to the database
   if (mysql_query(connection, sql_query))
   {
      printf("MySQL query error : %s\n", mysql_error(connection));
      exit(1);
   }

   return mysql_use_result(connection);
}

int main()
{
  MYSQL *conn;      // the connection
  MYSQL_RES *res;   // the results
  MYSQL_ROW row;    // the results row (line by line)

  struct connection_details mysqlD;
  mysqlD.server = "localhost";  // where the mysql database is
  mysqlD.user = "root";     // the root user of mysql   
  mysqlD.password = "123"; // the password of the root user in mysql
  mysqlD.database = "mysql";    // the databse to pick

  // connect to the mysql database
  conn = mysql_connection_setup(mysqlD);

  // assign the results return to the MYSQL_RES pointer
  res = mysql_perform_query(conn, "show tables");

  printf("MySQL Tables in mysql database:\n");
  while ((row = mysql_fetch_row(res)) !=NULL)
      printf("%s\n", row[0]);
   // clean up the database result set 
  mysql_free_result(res);
  // clean up the database link 
  mysql_close(conn);

  return 0;
}

但我收到以下错误::

r@r-desktop:~/mysqlC++$ g++ test.c -I/usr/include/mysql -I/usr/include/mysql++test.c: In function ‘int main()’:
test.c:47:19: warning: deprecated conversion from string constant to ‘char*’
test.c:48:17: warning: deprecated conversion from string constant to ‘char*’
test.c:49:21: warning: deprecated conversion from string constant to ‘char*’
test.c:50:21: warning: deprecated conversion from string constant to ‘char*’
test.c:56:48: warning: deprecated conversion from string constant to ‘char*’
/tmp/ccHFL1M4.o: In function `mysql_connection_setup(connection_details)':
test.c:(.text+0xf): undefined reference to `mysql_init'
test.c:(.text+0x51): undefined reference to `mysql_real_connect'
test.c:(.text+0x65): undefined reference to `mysql_error'
/tmp/ccHFL1M4.o: In function `mysql_perform_query(st_mysql*, char*)':
test.c:(.text+0xa2): undefined reference to `mysql_query'
test.c:(.text+0xb6): undefined reference to `mysql_error'
test.c:(.text+0xdd): undefined reference to `mysql_use_result'
/tmp/ccHFL1M4.o: In function `main':
test.c:(.text+0x170): undefined reference to `mysql_fetch_row'
test.c:(.text+0x18c): undefined reference to `mysql_free_result'
test.c:(.text+0x198): undefined reference to `mysql_close'
collect2: ld returned 1 exit status

我相信我做的一切都是对的..你们能指出我错的地方吗

最佳答案

您还必须实际链接库(-I 仅指定包含目录)。

尝试

g++ -I/usr/include/mysql -I/usr/include/mysql++ -L/usr/local/lib -lmysqlpp -lmysqlclient test.c

Here's a sample makefile for you .

下面是单独编译步骤的示例命令行(首先创建目标文件,然后将它们链接在一起):

g++ -I/usr/include/mysql -I/usr/include/mysql++ -o test.o -c test.c
g++ -L/usr/local/lib -lmysqlpp -lmysqlclient -o test test.o

关于c++ - 通过 C++ 连接到 mySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8896963/

相关文章:

c++ - 如何检查一组值中的一个是否相等?

c++ - 使用 std::enable_if 时如何拆分声明和实现

c++ - 在 C++ 中使用对数实现运算符重载

mysql - 我可以在 SQL UPDATE 中使用内部 SELECT 吗?

php - 在 myphp 中添加项目

c++ - mmap() 后出现段错误

linux - 为什么我不能在 Linux 上 ping 带有前导或尾随下划线的地址

c++ - 预分配 std::string 以传递到 WinAPI

mysql - 存储过程 - OUT 参数在选择时返回 null

Ruby:从/proc 获取 Linux 上的磁盘使用信息(或其他一些非阻塞且不会产生新进程的方式)