I am trying to setup the mysql connector for c++. When I try to compile this code this error appears /home/cjueden/programming projects/mysqlConnect/main.cpp|23|error: call of overloaded ‘get_driver_instance()’ is ambiguous
Please explain how to fix this as I am stumped.
CODE
#include <stdlib.h>
#include <iostream>
#include <mysql_connection.h>
#include <mysql_driver.h>
// #include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
using namespace sql::mysql;
int main(int argc, char const *argv[])
{
/* code */
cout << "STarting the MYSQL STUFF \n";
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://192.168.1.2:3306", "root", "");
/* Connect to the MySQL test database */
con->setSchema("test");
stmt = con->createStatement();
res = stmt->executeQuery("insert into cTest (text) value ('testing and stuff')");
cout << "inserted stuff\n";
res = stmt->executeQuery("SELECT 'cTest' AS _message");
while (res->next()) {
cout << "\t... MySQL replies: \n";
/* Access column data by alias or column name */
cout << res->getString("_message");
cout << "\t... MySQL says it again: \n";
/* Access column fata by numeric offset, 1 is the first column */
cout << res->getString(1);
}
return 0;
}
ERROR
/home/cjueden/programming projects/mysqlConnect/main.cpp||In function ‘int main(int, const char**)’:|
/home/cjueden/programming projects/mysqlConnect/main.cpp|23|error: call of overloaded ‘get_driver_instance()’ is ambiguous|
/home/cjueden/programming projects/mysqlConnect/main.cpp|23|note: candidates are:|
/usr/include/cppconn/driver.h|62|note: sql::Driver* get_driver_instance()|
/usr/include/mysql_driver.h|86|note: sql::mysql::MySQL_Driver* sql::mysql::get_driver_instance()
It looks like the version of get_driver_instance() in driver.h is in the global namespace, so here's one fix:
driver = ::get_driver_instance();
The compiler doesn't know which of the two functions to use. Based on what you're doing with the return value, I'm guessing you intended to use the one in driver.h.
Prepending double-colon (::) is a way to be more explicit when using stuff in the global namespace (like this function). It resolves the ambiguity.
Another fix would be to remove the earlier "using namespace sql::mysql;".
Related
When i dont use get_driver_instance i get segmentation fault.
I'm using linux and installed mysqlserver and connector using console commands. Any ideas how to fix this?
/usr/bin/ld: /tmp/ccXisL1A.o: in function main': test.cpp:(.text+0x5e): undefined reference to get_driver_instance'
collect2: error: ld returned 1 exit status
#include <stdlib.h>
#include <iostream>
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
int main()
{
std::cout << std::endl;
std::cout << "Running Hello World!"<< std::endl;
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "1234");
/* Connect to the MySQL test database */
con->setSchema("test1");
stmt = con->createStatement();
res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
while (res->next()) {
std::cout << "\t... MySQL replies: ";
/* Access column data by alias or column name */
std::cout << res->getString("_message") << std::endl;
std::cout << "\t... MySQL says it again: ";
/* Access column data by numeric offset, 1 is the first column */
std::cout << res->getString(1) << std::endl;
}
delete res;
delete stmt;
delete con;
}
i worked for 4 years on Java. Now im trying to learn C++. I want to create a Teamspeak3-Plugin.
First of all i tried to create a MySQL-Connection and i stuck on compiling.
I worked with the MySQL-Connector for C++ Version 8.0.20 and Boost for C++(for the connector) Version 1.72.0
It gives me the ErrorCode "C3867" which is saying "non standard syntax use & to create a pointer to member"
I dont know why i am getting this error and especially how to solve it.
Here are my classes:
mysql.h:
#ifndef mysql_H
#define mysql_H
#include <iostream>
#include <string>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
class mysql
{
public:
mysql();
int connect;
void createTables();
list<char*> getResult(char* qry);
};
#endif // !mysql_H
mysql.cpp
#include <iostream>
#include <string>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include "mysql.h"
using namespace std;
using namespace sql;
std::string host = "";
std::string port = "";
char* user = "";
char* password = "";
char* database = "";
sql::Driver *driver;
sql::Connection *con;
sql::ResultSet *res;
int connect() {
try {
driver = get_driver_instance();
std::string url = "tcp://" + host + ":" + port;
con = driver->connect(url, user, password);
cout << "MySQL connected!";
con->setSchema(database);
cout << "Database selected!";
return 0;
}
catch (sql::SQLException err) {
cout << "ERROR: " << err.what << endl;
}
}
void createTables() {
sql::Statement *stmt;
try {
stmt = con->createStatement();
stmt->executeQuery("CREATE TABLE IF NOT EXISTS JellyTest(Name VARCHAR(100), COINS INT)");
cout << "TableCreating done";
}
catch (sql::SQLException err) {
cout << "ERROR: " << err.what << endl;
}
}
list<char*> getResult(char* qry) {
sql::Statement *stmt;
sql::ResultSet *res;
list<char*> result;
try {
stmt = con->createStatement();
res = stmt->executeQuery(qry);
while (res->next) {
result.assign(res->next);
}
return result;
}
catch (sql::SQLException err) {
cout << "ERROR: " << err.what << endl;
}
}
Thanks for helping and have a nice day.
(I hope you are able to understand this text 'cause im from Germany)
So I finally got VS 2013 C++ to connect with mySQL using the Connector C and C++ and I know this because I am able to add a table into my DB using the stmt->execute() function.
Now, I am trying to get the rows using the ResultSet to retrieve from function executeQuery, but this error shows up:
Here is my code:
#include <iostream>
#include <string>
#include "mysql_connection.h"
#include "mysql_driver.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
#define dbHOST "tcp://127.0.0.1:3306"
#define dbUSER "root"
#define dbPASS "root"
#define dbDB "db_test"
using namespace std;
using namespace sql;
int main()
{
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
driver = sql::mysql::get_driver_instance();
con = driver->connect(dbHOST, dbUSER, dbPASS);
stmt = con->createStatement();
string query = "SELECT * FROM test";
res = stmt->executeQuery(query.c_str());
while (res->next()) {
cout << "id = " << res->getInt(1);
cout << ", label = '" << res->getString("label") << "'" << endl;
}
delete res;
delete stmt;
delete con;
system("pause");
return 0;
}
Under the output pane, it shows this as well:
First-chance exception at 0x77661D4D in MySQLConnect.exe: Microsoft C++ exception: sql::SQLException at memory location 0x0088FB5C.
Unhandled exception at 0x77661D4D in MySQLConnect.exe: Microsoft C++ exception: sql::SQLException at memory location 0x0088FB5C.
Under the call stack after I hit F5 and it breaks, it shows this:
MySQLConnect.exe!main() Line 35 C++
Line 35 is the line with res = stmt->executeQuery(query.c_str());
Let me know if you need more information on my code / settings.
Thanks in advance!
Silly me, I found out what is the problem.
I have to add in a line:
stmt->execute("USE " dbDB);
before I can do any further query for that database. The snippet is now like this:
stmt = con->createStatement();
stmt->execute("USE " dbDB); // <-- This line, change dbDB to your own
string query = "SELECT * FROM test"; // <- before all of these below
res = stmt->executeQuery(query.c_str());
I hope this helps!
I was expecting to see a short english sentence as output but i see hex value of it instread. I cannot find any information on this getblob function but i hear its what should be used for varchar columns. Before i used getString and it crashes the app, its funny though becuase it crashes after it successfully prints the sentence sometimes. However i cant have it crashing so i need to make it work with getblob, it returns an std::istream which i no nothing about. I experimented with converting istream to string but my lack of understanding of what an isteam is did not get me far.
#include <iostream>
#include <sstream>
#include <memory>
#include <string>
#include <stdexcept>
#include "cppconn\driver.h"
#include "cppconn\connection.h"
#include "cppconn\statement.h"
#include "cppconn\prepared_statement.h"
#include "cppconn\resultset.h"
#include "cppconn\metadata.h"
#include "cppconn\resultset_metadata.h"
#include "cppconn\exception.h"
#include "cppconn\warning.h"
#include "cppconn\datatype.h"
#include "cppconn\parameter_metadata.h"
#include "mysql_connection.h"
#include "mysql_driver.h"
using namespace std;
int main()
{
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
// ...
driver = sql::mysql::get_mysql_driver_instance();
cout<<"Connecting to database...\n";
con = driver->connect("tcp://xx.xxx.xxx.xxx:3306", "xxxxx", "xxxxxx");
sql::ResultSet *res;
// ...
stmt = con->createStatement();
// ...
con->setSchema("mrhowtos_main");
res = stmt->executeQuery("SELECT english FROM `eng` WHERE `ID` = 16;");
while (res->next()) {
istream *stream = res->getBlob(1);
string s;
getline(*stream, s); //crashes here - access violation
cout << s << endl;
}
cout<<"done\n";
delete res;
delete stmt;
delete con;
system("PAUSE");
return 0;
}
UPDATE: crashes on getline
value of stream after getblob:
stream 0x005f3e88 {_Chcount=26806164129143632 } std::basic_istream > *
(This answered the first version of this question, which had a cout << stream << endl; statement.)
You're printing an istream *.
I suppose you would like to read from that istream and print what you read, e.g. with
string s;
while (getline(*stream, s))
cout << s << endl;
I am using the following code and I get this error
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '??]?j?X?0' at line 1
My code is as follows this works perfectly in the debug build
#include <iostream>
#include <vector>
//#define CPPCONN_PUBLIC_FUNC
/* MySQL Connector/C++ specific headers */
#include <driver.h>
#include <connection.h>
#include <statement.h>
#include <prepared_statement.h>
#include <resultset.h>
#include <metadata.h>
#include <resultset_metadata.h>
#include <exception.h>
int main()
{
sql::Driver *driver;
sql::Statement *stmt;
sql::ResultSet *res;
sql::Connection *con;
sql::PreparedStatement *prep_stmt;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("server", "user", "pass");
/* Connect to the MySQL database */
con->setSchema("MySchema");
std::vector<std::string> SymbolList;
std::string SQL = "SELECT `MyList`.`Symbol` FROM `MySchema`.`MasterList`;";
try
{
stmt = con->createStatement();
res = stmt->executeQuery(SQL);
while(res->next()) //If object exists
{
std::string symbol = res->getString("Symbol");
SymbolList.push_back(symbol);
std::cout << symbol;
}
}
catch(std::exception &ex)
{
std::string d = ex.what();
}
std::cin.get();
return 0;
}//end method
I am still confused by the error I am getting the code seems to work fine in debug mode but in release mode it gives this error out. Any suggestions on what I should try and what I might be doing wrong. I am using VS2010.I can also upload a small project that I created