c++ - mysql-connector-c++ - ‘get_driver_instance’ 不是 ‘sql::mysql’ 的成员

标签 c++ mysql g++

我是 C++ 的初学者,我认为我要学习的唯一方法就是编写一些代码。我正在尝试构建一个连接到 mysql 数据库的程序。我在 Linux 上使用 g++。没有想法。

我运行“make”,这是我的错误:

hello.cpp:38: error: ‘get_driver_instance’ is not a member of ‘sql::mysql’
make: *** [hello.o] Error 1

这是我的代码,包括 makefile。任何帮助都会很棒!提前致谢

###BEGIN hello.cpp###

#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <stdexcept>

#include "mysql_connection.h"

#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>

#define EXAMPLE_HOST "localhost"
#define EXAMPLE_USER "root"
#define EXAMPLE_PASS ""
#define EXAMPLE_DB "world"

using namespace std;
using namespace sql::mysql;

int main(int argc, const char **argv)
{   

    string url(argc >= 2 ? argv[1] : EXAMPLE_HOST);
    const string user(argc >= 3 ? argv[2] : EXAMPLE_USER);
    const string pass(argc >= 4 ? argv[3] : EXAMPLE_PASS);
    const string database(argc >= 5 ? argv[4] : EXAMPLE_DB);

    cout << "Connector/C++ tutorial framework..." << endl;
    cout << endl;


    try { 
        sql::Driver *driver;
        sql::Connection *con;
        sql::Statement  *stmt;
        driver = sql::mysql::get_driver_instance();
        con = driver->connect("tcp://127.0.0.1:3306", "user", "password");
        stmt = con->createStatement();
        stmt->execute("USE " EXAMPLE_DB);
        stmt->execute("DROP TABLE IF EXISTS test");
        stmt->execute("CREATE TABLE test(id INT, label CHAR(1))");
        stmt->execute("INSERT INTO test(id, label) VALUES (1, 'a')");
        delete stmt;
        delete con;

    } catch (sql::SQLException &e) {
        /*
          The MySQL Connector/C++ throws three different exceptions:

          - sql::MethodNotImplementedException (derived from sql::SQLException)
          - sql::InvalidArgumentException (derived from sql::SQLException)
          - sql::SQLException (derived from std::runtime_error)
        */
        cout << "# ERR: SQLException in " << __FILE__;
        cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
        /* Use what() (derived from std::runtime_error) to fetch the error message */
        cout << "# ERR: " << e.what();
        cout << " (MySQL error code: " << e.getErrorCode();
        cout << ", SQLState: " << e.getSQLState() << " )" << endl;

        return EXIT_FAILURE;
    }

    cout << "Done." << endl;
    return EXIT_SUCCESS;
}
###END hello.cpp###

###BEGIN Make File###
SRCS     := hello.cpp
OBJS     := $(SRCS:.cpp=.o)
CXXFLAGS := -Wall -pedantic
INCPATHS := -I/home/user/mysql-connector/include/
LIBPATHS := -L/home/user/mysql-connector/lib/ -L/home/user/mysql-connector-c/lib/
LIBS     := -static -lmysqlclient -mysqlcppconn-static
EXE      := MyExecutable



$(EXE): $(OBJS)
    $(CXX) $(OBJS) $(LIBPATHS) $(LIBS) -o $@

.cpp.o:
    $(CXX) $(CXXFLAGS) $(INCPATHS) -c $< -o $@

###End Makefile###

最佳答案

你的问题在这里,get_driver_instance 不是 sql::mysql 的成员,所以要解决这个问题,删除 sql::mysql::它会起作用

改变这一行

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

对此

 driver = get_driver_instance();

查看 examples这里

关于c++ - mysql-connector-c++ - ‘get_driver_instance’ 不是 ‘sql::mysql’ 的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3024911/

相关文章:

PHP-Mysql : storing images in DB - escaping special characters

gcc - 如何包含使用 GCC C++ 编译器编译的*项目根*头文件?

C++ 对线程感到困惑

c++ - gcc 抑制警告 "too small to hold all values of"

c++ - 无法部署 GLFW 3.2

C++ : How to initialize an object that uses two header files?

php - 如果我在使用 Mysql 的 Laravel 中使用 Query Builder 而不是 Eloquent ORM,模型仍然有用吗?

mysql - 将 mysql 表与查询 UNION 或 JOIN 结合起来?

vim - 如何在 VIM 中运行 g++ 并在水平分割中显示结果

c++ - 构建 64 位库