Cant create MySQL-Connection - Stuck at compiling - c++

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)

Related

Problem with MYSQL Connector/C++: crashes before all

#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>
#include <cppconn/prepared_statement.h>
using namespace std;
int testsql() {
cout << endl;
cout << "Let's have MySQL count from 10 to 1..." << endl;
sql::Driver *driver;
sql::Connection *con;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "37975");
delete con;
cout << endl;
return EXIT_SUCCESS;
}
int main(void)
{
cout << "Hello world!" << endl;
}
The above is the codes I copied from the Internet. It compiles but crashes when running. I use MinGW-W64. When I run the program under Eclipse, the program terminated with exit value -1,073,741,515. The mysql connector is Mysql Connector/C++ 1.1.13. I have no idea. Can some one give me some hint?
Thank you,

VS Express 2013 C++ with MySQL Connector stmt->executeQuery() error

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!

c++ sql connection "get_driver_instance(); is undefined " error

MyCode is ;
#include<iostream>
using namespace std;
#include <string>
#include "mysql_connection.h"
#include <stdlib.h>
#include <iomanip>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
int main(){
cout<<"sadullah";
string database = "sch1667138";
string ipAdress = "144.122.71.165";
string password = "???";
string userName = "???";
sql::Driver *driver;
sql::Connection *con;
try {
driver =get_driver_instance();
con = driver->connect("tcp://" + ipAdress + ":3306", userName,
password);
con->setSchema("sch1667138");
cout << "successfully connected to db...." << endl;
} catch (sql::SQLException &e) {
cout << e.what() <<endl;
}
return 0;
}
When I run the code,I take the message "reference undefined get_driver_instance();" What should I do
I solved the problem. The reason is that I am working on Ubuntu, I should have run it from terminal, with the code ; g++ -o sado 1667138.cpp -lmysqlcppconn sadu is the name of execute file, and -lmysqlcppconn is adding the mysql library to cpp

Linker error 1120 in VC++ 2010 project.

I'm doing my first VC++ project and am using the following code. The project has the following files.
Project1.cpp
#include "stdafx.h"
#include <iostream>
#include "PopulateDB.h"
using namespace std;
int main (void)
{
PopulateDB x;
x.calcUpload();
return 0;
}
PopulateDB.h
#pragma once
#include "mysql_connection.h"
#include <cppconn/driver.h>
class PopulateDB
{
public:
int calcUpload(void);
PopulateDB(void);
~PopulateDB(void);
private:
int updateMA(sql::Connection &);
};
PopulateDB.cpp
#include "StdAfx.h"
#include "PopulateDB.h"
#include <iostream>
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include "Technical.h"
using namespace std;
int updateMA(sql::Connection& con)
{
sql::Statement *stmt;
sql::ResultSet *res;
int i;
stmt = con.createStatement();
res = stmt->executeQuery("SELECT * from PriceAMS");
while(res->next())
{
cout << "Symbol " << i << " = " << res->getBlob("Symbol") << endl;
i++;
}
delete stmt;
}
int calcUpload(void)
{
cout << "Running Connection..." << endl;
try
{
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", "nishantd", "mySQLDB%passWord2013"); //we can initialize the user/pass in constructor
/* Connect to the MySQL test database */
con->setSchema("testMMDB");
cout << "Looks like it connected..." << endl;
updateMA(*con);
delete con;
}
catch (sql::SQLException &e)
{
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
return 0;
}
PopulateDB::PopulateDB(void)
{
}
PopulateDB::~PopulateDB(void)
{
}
Technical.h
#pragma once
#include <vector>
class Technical
{
public:
float StandardMovingAverage(std::vector<float> values);
float StdDev(std::vector<float> values);
float Variance(std::vector<float> values);
int RSI(std::vector<float> values);
Technical(void);
~Technical(void);
};
Technical.cpp
#include "StdAfx.h"
#include "Technical.h"
#include <cmath>
#include <vector>
using namespace std;
float Technical::StandardMovingAverage(vector <float> values)
{
int sum=0;
for(int i=0; i < values.size(); i++)
sum+=values[i];
return sum/values.size();
}
float Technical::StdDev(vector <float> values)
{
float E=0;
float ave = StandardMovingAverage(values); //this function just calculates the mean values
for(int i=0; i < values.size(); i++)
E+=(values[i]- ave)*(values[i]- ave);
return sqrt(1/values.size()*E);
}
float Technical::Variance(vector <float> values)
{
return StdDev(values)*StdDev(values);
}
Technical::Technical(void)
{
}
Technical::~Technical(void)
{
}
I am getting the following error in VC++ 2010. There are warnings which I am currently ignoring
1>Project1.obj : error LNK2001: unresolved external symbol "public: int __thiscall PopulateDB::calcUpload(void)" (?calcUpload#PopulateDB##QAEHXZ)
1>D:\Project\CPP\Project1\Release\Project1.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I am trying to resolve this and get my code to work. Its even more frustrating because I suspect I am missing something very basic since I am new to VC++ and the code anyway doesn't do much in its current shape.
Really appreciate all help
Thanks,
Nick
You are calling a function named PopulateDB::calcUpload - a member function of class PopulateDB. But you've never actually implemented that function. You did implement a different, standalone, non-member function named ::calcUpload, but you are not calling it.
In your implementation file, make it
int PopulateDB::calcUpload() {...}

Ambiguous function call to itself

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;".