我是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###
答案 0 :(得分:3)
你的问题在这里,get_driver_instance不是sql :: mysql的成员,所以修复这个删除sql :: mysql ::它会起作用
更改此行
driver = sql::mysql::get_driver_instance();
到这个
driver = get_driver_instance();
查看此处的examples
答案 1 :(得分:1)
包括:??
#include "mysql_driver.h"
答案 2 :(得分:1)
最后我可以在Ubuntu 12.04中使用C ++连接器成功编译程序我已经使用此命令安装了连接器
'apt-get install libmysqlcppconn-dev'
最初,我遇到了同样的问题,“未定义引用`get_driver_instance'”来解决这个问题我声明了我的MySQL_Driver类型的驱动程序实例变量。对于准备参考,此类型在mysql_driver.h文件中定义。这是我在程序中使用的代码片段。
sql::mysql::MySQL_Driver *driver;
try {
driver = sql::mysql::get_driver_instance();
}
我用-l mysqlcppconn链接器选项
编译了程序并且不要忘记包含此标题
#include "mysql_driver.h"
答案 3 :(得分:0)
看起来该函数在全局命名空间中,不在sql::mysql::