Connecting to MySQL From Eclipse (CDT) - c++

I tried to connect to MySQL database by C++ in Eclipse with all the recommendations that are on the internet, but none of them are fully helpful. I am using Eclipse with MinGW to connect to MySQL.
I have added the:
C:\Program Files\boost
C:\Program Files\MySQL\MySQL Connector C++ 1.1.3\include
C:\Program Files\MySQL\MySQL Server 5.6\include
to the include directory and
C:\Program Files\MySQL\MySQL Server 5.6\lib
C:\Program Files\MySQL\Connector C++ 1.1.2\lib\opt
to Library Directories (-L). Also I added the
libmysql.lib
mysqlcppconn-static.lib
to Additional Dependencies (-l).
The code is:
#include <stdlib.h>
#include <iostream>
#include "mysql_driver.h"
#include "mysql_connection.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(void)
{
cout << endl;
cout << "Running 'SELECT 'Hello World!' AS _message'..." << 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", "root", "root");
/* Connect to the MySQL test database */
con->setSchema("test");
stmt = con->createStatement();
res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
while (res->next()) {
cout << "\t... MySQL replies: ";
/* Access column data by alias or column name */
cout << res->getString("_message") << endl;
cout << "\t... MySQL says it again: ";
/* Access column fata by numeric offset, 1 is the first column */
cout << res->getString(1) << endl;
}
delete res;
delete stmt;
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;
}
cout << endl;
return EXIT_SUCCESS;
}
But none of them seems to work and lots of errors pops up. For now I get an error on
"call of overloaded 'get_driver_instance()' is ambiguous" Does any of you guys have an experience to connect to MySQL through Eclipse in C++ environment. I am not a Linux user and the tutorial on Linux didn't work either.

Just substitute this
driver = get_driver_instance();
by this
driver = sql::get_driver_instance();
it's only a namespace problem;

Related

Visual Studio 2019 error on build C++ MySQL connector

I downloaded libraries and headers from Connector/C++ 8.0.26 and tried all of them one by one, But always get error on building point.
I used Visual Studio 2019 (Version 16.10.0) and created a simple console app based on example from MySQL website. The original example is like this:
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
int main(void)
{
cout << endl;
cout << "Running 'SELECT 'Hello World!' »
AS _message'..." << 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", "root", "root");
/* Connect to the MySQL test database */
con->setSchema("test");
stmt = con->createStatement();
res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
while (res->next()) {
cout << "\t... MySQL replies: ";
/* Access column data by alias or column name */
cout << res->getString("_message") << endl;
cout << "\t... MySQL says it again: ";
/* Access column data by numeric offset, 1 is the first column */
cout << res->getString(1) << endl;
}
delete res;
delete stmt;
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;
}
cout << endl;
return EXIT_SUCCESS;
}
I got same error with different configuration (32,64,Debug|release ....):
I read more than +100 topics here, also in other sites about same problem as well and try every solution that they provided but none of them work.
The only solution I not checked yet is to download the VS 2015 and try with that.
Now we are in 2021, is this really the right solution to create a C++/MySQL simple APP or I have to do something else?
After checking all solution around the internet finally the problem solved by copying all .dll files to the c:/windows !

c++ connect to online database

I'm trying to figure out how to connect to an online database with c++. I've tried to google but I can only find ways to connect to the localhost.
The code I have is as following:
/* Standard C++ includes */
#include <stdlib.h>
#include <iostream>
/*
Include directly the different
headers from cppconn/ and mysql_driver.h + mysql_util.h
(and mysql_connection.h). This will reduce your build time!
*/
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
int main(void)
{
cout << endl;
cout << "Running 'SELECT 'Hello World!' 'AS _message'..." << 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", "root", "root");
// Connect to the MySQL test database
con->setSchema("test");
stmt = con->createStatement();
res = stmt->executeQuery("SELECT 'Hello World!'"); // replace with your statement
while (res->next()) {
cout << "\t... MySQL replies: ";
// Access column data by alias or column name
cout << res->getString("_message") << endl;
cout << "\t... MySQL says it again: ";
// Access column fata by numeric offset, 1 is the first column
cout << res->getString(1) << endl;
}
delete res;
delete stmt;
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;
}
cout << endl;
return EXIT_SUCCESS;
}
The line con = driver->connect("tcp://127.0.0.1:3306", "root", "root"); is what connects to a mysql database on my pc (I think??). How do I connect to an online mysql database instead? I have a free webhost site where the database is at and I'd like to throw queries at it. Do I need a completely different library or something?

Connect from c++ to mysql on Ubuntu machine

I have MySQL running on a virtual machine of Ubuntu OS.
I want to read and write data to MySQL using c++ with Windows 7 OS, I have few problems:
How do I know the IP address of MySQL server? Is it the IP of the Ubuntu VM or 127.0.0.1?
How can I read data using c++? Do I need a driver or anything else?
I tried to; install Visual Studio Express 2010 c++, install MySQL on Ubuntu.
Also I tried this code: from oracle site.
/*
Include directly the different
headers from cppconn/ and mysql_driver.h + mysql_util.h
(and mysql_connection.h). This will reduce your build time!
*/
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
int main(void)
{
cout << endl;
cout << "Running 'SELECT 'Hello World!' »";
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", "root", "root");
/* Connect to the MySQL test database */
con->setSchema("test");
stmt = con->createStatement();
res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
while (res->next()) {
cout << "\t... MySQL replies: ";
/* Access column data by alias or column name */
cout << res->getString("_message") << endl;
cout << "\t... MySQL says it again: ";
/* Access column fata by numeric offset, 1 is the first column */
cout << res->getString(1) << endl;
}
delete res;
delete stmt;
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;
}
cout << endl;
return EXIT_SUCCESS;
}

Connection Crash in mysql C++

Currently, I have implemented a program to write to a mysql database... I am using Xcode with c++as well. I have imported all the libraries correctly but I'm getting a BAD EXCESS error message on this line :
con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
What seems to be the issue? Heres the full program...
#include <stdlib.h>
#include <iostream>
// Include directly the different
// headers from cppconn/ and mysql_driver.h + mysql_util.h
// (and mysql_connection.h). This will reduce your build time!
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
int main(void)
{
cout << endl;
cout << "Running 'SELECT 'Hello World!' AS _message'..." << 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", "root", "root");
// Connect to the MySQL test database
con->setSchema("test");
stmt = con->createStatement();
res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
while (res->next()) {
cout << "\t... MySQL replies: ";
// Access column data by alias or column name
cout << res->getString("_message") << endl;
cout << "\t... MySQL says it again: ";
// Access column fata by numeric offset, 1 is the first column
cout << res->getString(1) << endl;
}
delete res;
delete stmt;
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;
}
cout << endl;
return EXIT_SUCCESS;
}
EDIT So currently, I looked at this topic which is almost exactly similar to mine but I am still so lost... How would I recompile mysql with cmake and change such flags?
I have been at this issue for several hours and no coding has really been done... Just staring at my screen with a face >:(.
Here's a similar problem (almost identical?), have you checked it?
MySQL Connector/C++ BAD ACCESS crash

MySQL Connector/C++ Example code is not working

Hello i've just started using C++ MySql Connector in appication
Release|Win3
My Errors:
Warning 1 warning C4627: '#include ': skipped when looking
for precompiled header use c:\Documents and Settings...
Warning 2 warning C4627: '#include ': skipped when looking
for precompiled header use c:\Documents and Settings...
Error 3 fatal error C1083: Cannot open include file:
'boost/variant.hpp': No such file or directory C:\Program
Files\MySQL\MySQL Connector C++
1.1.0\include\cppconn\connection.h 29 lacznik
#include <cstdlib>
#include <iostream>
#include "stdafx.h"
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
int main(void)
{
cout << endl;
cout << "Running 'SELECT 'Hello World!' »
AS _message'..." << 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", "root", "root");
/* Connect to the MySQL test database */
con->setSchema("test");
stmt = con->createStatement();
res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
while (res->next()) {
cout << "\t... MySQL replies: ";
/* Access column data by alias or column name */
cout << res->getString("_message") << endl;
cout << "\t... MySQL says it again: ";
/* Access column fata by numeric offset, 1 is the first column */
cout << res->getString(1) << endl;
}
delete res;
delete stmt;
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;
}
cout << endl;
return EXIT_SUCCESS;
}
Warning 1 warning C4627: '#include ': skipped when looking for precompiled header use c:\Documents and Settings...
means that the "precompiled header" include
#include "stdafx.h"
should be the first header included.