简体   繁体   English

如何在 Ubuntu 上将 MySQL 数据库连接到 C++?

[英]How to connect a MySQL database to C++ on Ubuntu?

I am trying to make a database management system in C++, however I have been struggling for many hours to even connect the MySQL to my program.我正在尝试用 C++ 制作一个数据库管理系统,但是我已经努力了好几个小时才能将 MySQL 连接到我的程序。

I am fairy new to C++, but I have experience in Java, and it was never such a pain connecting to MySQL.我是 C++ 的新手,但我有 Java 方面的经验,连接 MySQL 从来没有这么痛苦过。

I have tried over a dozen variations of the code below, yet I still have not succeeded.我已经尝试了以下代码的十几种变体,但我仍然没有成功。

How do I go about connecting a MySQL database to C++ for Linux?如何将 MySQL 数据库连接到 Linux 的 C++?

My code at the moment looks like this:我现在的代码是这样的:

#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;
}

And these are the errors I receive:这些是我收到的错误:

error: 'get_mysql_driver_instance' is not a member of 'sql::mysql::MySQL_Driver' driver = sql::mysql::MySQL_Driver::get_mysql_driver_instance();错误:'get_mysql_driver_instance' 不是 'sql::mysql::MySQL_Driver' 驱动程序的成员 = sql::mysql::MySQL_Driver::get_mysql_driver_instance();

CMakeFiles/SuperStorage.dir/build.make:62: recipe for target 'CMakeFiles/SuperStorage.dir/main.cpp.o' failed make[3]: * [CMakeFiles/SuperStorage.dir/main.cpp.o] Error 1 CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/SuperStorage.dir/all' failed make[2]: CMakeFiles/SuperStorage.dir/build.make:62: 目标 'CMakeFiles/SuperStorage.dir/main.cpp.o' 的配方失败 make[3]: * [CMakeFiles/SuperStorage.dir/main.cpp.o] 错误 1 CMakeFiles/Makefile2:67:目标“CMakeFiles/SuperStorage.dir/all”的配方失败 make[2]: [CMakeFiles/SuperStorage.dir/all] Error 2 CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/SuperStorage.dir/rule' failed make[1]: [CMakeFiles/SuperStorage.dir/all] 错误 2 CMakeFiles/Makefile2:79:目标“CMakeFiles/SuperStorage.dir/rule”的配方失败 make[1]: [CMakeFiles/SuperStorage.dir/rule] Error 2 Makefile:118: recipe for target 'SuperStorage' failed make: * [SuperStorage] Error 2 [CMakeFiles/SuperStorage.dir/rule] 错误 2 Makefile:118: 目标 'SuperStorage' 的配方失败 make: * [SuperStorage] 错误 2

This is wrong:这是错误的:

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

It should be:它应该是:

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

This is clearly shown in the old MySQL C++ Connector tutorial page for connecting to a database (though the 8.0 documentation seems to have removed all usage examples 😢).在用于连接数据库的旧 MySQL C++ 连接器教程页面中清楚地显示(尽管 8.0 文档似乎已删除所有使用示例😢)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM