Mysql-connector-C ++ - 'get_driver_instance is not a member of' sql :: mysql

I start in C ++ and decided that the only way I'm going to learn is to pollute some code. I am trying to create a program that connects to a mysql database. I am using g ++ on linux. Without an ideal.

I run "make" and this is my mistake:

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] : 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### 
+6
c ++ mysql g ++
source share
4 answers

Your problem is here, get_driver_instance is not a member of sql :: mysql, so to fix this delete sql :: mysql :: and it will work

Change this line

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

to that

  driver = get_driver_instance(); 

check out the examples here

+2
source share

Include: ??

 #include "mysql_driver.h" 
+1
source share

It looks like this function is in the global namespace, do not add sql::mysql::

0
source share

Finally, I was able to successfully compile a program with a C ++ connector in Ubuntu 12.04. I installed the connector using this command

 'apt-get install libmysqlcppconn-dev' 

Initially, I ran into the same problem as the "undefined link to get_driver_instance '" to solve this problem. I declare my driver instance variable of type MySQL_Driver. For help, this type is defined in mysql_driver.h. Here is a piece of code that I used in my program.

 sql::mysql::MySQL_Driver *driver; try { driver = sql::mysql::get_driver_instance(); } 

and I compiled the program using the -l option mysqlcppconn linker

and don't forget to include this header

 #include "mysql_driver.h" 
0
source share

All Articles