I am a beginner at c++ and figured the only way I am going to learn is to get dirty with some code. I am trying to build a program that connects to a mysql database. I am using g++, on linux. With no ide.
I run "make" and this is my error:
hello.cpp:38: error: ‘get_driver_instance’ is not a member of ‘sql::mysql’
make: *** [hello.o] Error 1
Here is my code including makefile. Any Help would be great! Thanks in advance
###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] : EXA开发者_JS百科MPLE_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###
Your problem is here , get_driver_instance isn't a member of sql::mysql,so to fix this removing sql::mysql:: and it will work
Change this line
driver = sql::mysql::get_driver_instance();
to this
driver = get_driver_instance();
check out the examples here
Include : ??
#include "mysql_driver.h"
Finally I could successfully compile a program with C++ connector in Ubuntu 12.04 I have installed the connector using this command
'apt-get install libmysqlcppconn-dev'
Initially I faced the same problem with "undefined reference to `get_driver_instance' " to solve this I declare my driver instance variable of MySQL_Driver type. For ready reference this type is defined in mysql_driver.h file. Here is the code snippet I used in my program.
sql::mysql::MySQL_Driver *driver;
try {
driver = sql::mysql::get_driver_instance();
}
and I compiled the program with -l mysqlcppconn linker option
and don't forget to include this header
#include "mysql_driver.h"
Looks like that function is in the global namespace, don't prepend with sql::mysql::
精彩评论