如何将MySQL数据库连接到Ubuntu上的C

How to connect a MySQL database to C++ on Ubuntu?

本文关键字:Ubuntu 上的 数据库连接 MySQL      更新时间:2023-10-16

我正在尝试在C 中建立一个数据库管理系统,但是我一直在努力工作很多小时,甚至将MySQL连接到我的程序。

我是C 的童话,但我在Java上有经验,与MySQL相关的痛苦从来都不是很痛苦。

我尝试了以下代码的十几个变体,但我仍然没有成功。

如何将MySQL数据库连接到Linux的C ?

目前我的代码看起来像:

#include <iostream>
#include <stdlib.h>
#include <string>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <mysql_driver.h>
using namespace std;
int main() {
    sql::mysql::MySQL_Driver *driver;
    sql::Connection *con;
    sql::ConnectOptionsMap connection_properties;
    /* Create a connection */
    driver = sql::mysql::MySQL_Driver::get_mysql_driver_instance();
    con = driver->connect("tcp://127.0.0.1:3306", "root", "rootpass");
    /* Connect to the MySQL test database */
    con->setSchema("test");
    delete con;
    return 0;
}

,这些是我收到的错误:

错误:‘get_mysql_driver_instance'不是'sql :: mysql :: mysql_driver'的成员 驱动程序= sql :: mysql :: mysql_driver :: get_mysql_driver_instance((;

cmakefiles/superstorage.dir/build.make:62:目标'cmakefiles/superstorage.dir/main.cpp.o'失败的食谱制作[3]:* [CMAKEFIELS/SUPERSTREADOK.DIR/MAIN.CPP.O]错误1cmakefiles/makefile2:67:目标'cmakefiles/superstorage.dir/all'失败的食谱制作[2]: [cmakefiles/superstorage.dir/all]错误2cmakefiles/makefile2:79:目标'cmakefiles/superstorage.dir/rule'失败制作[1]: [cmakefiles/superstorage.dir/规则]错误2makefile:118:目标"超级恒星"的食谱失败制作: * [Superstorage]错误2

这是错误的:

driver = sql::mysql::MySQL_Driver::get_mysql_driver_instance();

应该是:

driver = sql::mysql::get_mysql_driver_instance();

这在用于连接到数据库的旧MySQL C 连接器教程页面中清楚地显示了这一点(尽管8.0文档似乎已删除了所有用法示例 (。