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!
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)
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;".
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
I've stuck with a problem, trying to insert text as blob value, MySQL C++ connector crashed with exception: "Access violation reading location". I've seen questions like this here, but none has been answered. Here is a code sample:
#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>
#include <string>
#include <iostream>
#include <sstream>
using namespace sql;
void main(){
Connection *dbConnection;
Driver *driver;
Connection *con;
driver = get_driver_instance();
con = driver->connect("localhost", "root", "");
Statement *stmt = con->createStatement();
try{
stmt->execute("USE test");
stmt->execute("DROP TABLE blob_test");
stmt->execute("CREATE TABLE blob_test("
"id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,"
"original BLOB NOT NULL);");
char smallBlob[32];
for (char i = 0; i < sizeof(smallBlob); ++i)
{
smallBlob[i] = i;
}
std::istringstream str(smallBlob);
PreparedStatement *pstmt = con->prepareStatement("INSERT INTO blob_test(id, original) VALUES (1, ?)");
pstmt->setBlob( 1, &str);
pstmt->executeUpdate();
}catch(sql::SQLException &e){
std::cerr << "# ERR: " << e.what();
std::cerr << " (MySQL error code: " << e.getErrorCode();
std::cerr << ", SQLState: " << e.getSQLState() << " )" << std::endl;
}
}
Thanks in advance!
Problem solved compiling MySQL cpp connector from sources.
If you go that way read carefully this and this (specially comments in the end). I also had to changed a couple of lines in config.h to implicit cast of int to char*. Also be attentive don't mix Debug and Release, for example if you compile your application in Debug, you should link Debug version of MySQL connector and vice-a-verse.
After I linked lib that I had compiled everything launched )
You may read on dev.mysql.com forums that in most cases it's the only way force the connector to work. While compiling I met with the thing that prove this, include files in source code is different from includes in windows binary distributive.