I am trying to establish connection with MySQL database in C++ project.And I followed the instructions given in link. After installing Cmake, I was facing problem in Build & Install MySQL Connector/C++.Because CmakeLists.txt does not exists in directory.After some search on web, I have found this answer and followed the steps given, but it didn't work in my case.
Frustrated will all this, I copied all the files(Extracted from dowload link of MySQLConnector C++) into my project folder, and trying to establish connection using this piece of code.
/* Standard C++ includes */
#include <cstdlib>
#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 "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;
void db_connection() {
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"); // 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;
}
But it says, undefined reference toget_driver_instance', but it is defined indriver.h`.
Finally, i have found the solution :)
First I completely un-installed MySQL from system (MySQL Client, MYSQL Server and libmysqlcppconn-dev).
After that, install all these again again.
Install MySQL Client and Server.
sudo apt-get install libmysqlcppconn-dev
Now ensure you have all dependencies installed
sudo apt-get build-dep libmysqlcppconn-dev
And now build the package using -lmysqlcppconn.
Related
I've seen some similar questions but most are based around PHP, there was one based around C but the only answer was go to the SQLConnect() docs, which I've already done.
https://learn.microsoft.com/en-us/sql/odbc/reference/syntax/sqlconnect-function?redirectedfrom=MSDN&view=sql-server-ver15
Seems to be little content online, also I'm a beginner so excuse any silly issues.
I set up the SQL database using the MYSQL Workbench and it's sitting on my localhost:3306. I was able to connect it to the ODBC Data Source Admin program successfully.
So I believe I'm using the right function and have the right SQL back end set up, I just don't know where to go from here.
The end goal is to be able to 'insert' records into my database with the program and also 'select' from it too.
What am I doing wrong, or what am I missing?
# include <stdio.h>
# include <stdlib.h>
# include <sql.h>
# include <sqlext.h>
# include <cstdio>
# include <cstdint>
using namespace std;
int main(){
SQLHENV henv = NULL;
SQLHDBC hdbc = NULL;
/* Initialize the ODBC environment handle. */
SQLAllocHandle(SQL_HANDLE_ENV, NULL, &henv);
/* Set the ODBC version to version 3 (the highest version) */
SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
(void*)SQL_OV_ODBC3, 0);
/* Allocate the connection handle. */
SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
SQLConnectW(hdbc, (SQLWCHAR*)"localhost", (SQLSMALLINT) 9,
(SQLWCHAR*)"root", (SQLSMALLINT)4,
(SQLWCHAR*)"password", (SQLSMALLINT)8);
return(0);
}
I have added the comments inside the code, which will help you to understand the code easily.
Credits: Link
/* Includes Standard C++ */
#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 'Successfull' »
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 'Successfull' AS _message"); // 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;
}
I have this simple C++ program:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: main.cpp
* Author: marek
*
* Created on 1. listopadu 2017, 22:33
*/
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
/*
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 <mysql_driver.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
cout << endl;
cout << "running select as a messege" << 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://10.0.0.6:3306", "root", "*****");
/* Connect to the MySQL test database */
con->setSchema("mysql");
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;
std::cout << "hello world pc 5 mysql\n";
#ifdef __linux__
std::cout << "__linux__\n";
#elif defined(__unix__)
std::cout << "__unix__\n";
#elif defined(_WIN64)
std::cout << "_WIN64\n";
#elif defined(_WIN32)
std::cout << "_WIN32\n";
#endif
#if __WORDSIZE == 64
std::cout << "64 bit\n";
#else
std::cout << "32 bit\n";
#endif
return 0;
}
Its important to note:
MySQL server (MariaDB) is running on Raspi Pi 3 on local subnet.
I am able to connect to DB for example from HeidiSQL
The same script is build build successfully on Raspi pi 3
My IDE is NetBeans with following settings:
Tools > options > C/C++ compilers is MiniGW QT with everything store in default installation folder. For me C:\Qt\Qt5.9.2\, and make command is C:\msys\1.0\bin\make.exe.
To this compiller I am also including directories Mysql connector C++ 1.1.9. also in the default installation directory. For me it would be c:\Program Files\MySQL\MySQL Connector C++ 1.1.9
My Windows path is set to:
C:\ProgramData\Oracle\Java\javapath;
%SystemRoot%\system32;
%SystemRoot%;
%SystemRoot%\System32\Wbem;
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;
C:\Program Files\PuTTY\;
c:\msys\1.0\bin\;
C:\MinGW\bin;
c:\Qt\Qt5.9.2\5.9.2\mingw53_32\bin\;
c:\Qt\Qt5.9.2\5.9.2\;
c:\Qt\Qt5.9.2\5.9.2\mingw53_32\bin\designer.exe;
c:\Qt\Qt5.9.2\Tools\QtCreator\bin\;
c:\Qt\Qt5.9.2\Tools\QtCreator\bin\qtcreator.exe;
C:\Program Files\MariaDB\MariaDB Connector C\lib\;
C:\Program Files\MariaDB\MariaDB Connector C\lib\plugin\;
C:\Program Files\MySQL\MySQL Utilities 1.6\;
c:\Program Files\MySQL\MySQL Connector C++ 1.1.9\include\;
After a long struggle I've been able to make a build on NetBeans Windows. Inside of the same folder, where my executable is being stored I copied in a mysqlcppconn.dll, which was originally missing.
If I run this version of my program from Netbeans I get:
running select as a messege
RUN FAILED (exit value 255, total time: 2s)
Windows CMD I get:
nothing.
just windows dialog of program crashes
I want to connect my Raspberry Pi 2 to an external MySQL database on 000webhost.com with the help of C++.
#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>
using namespace std;
int main(void)
{
try {
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("<000webhost mysql address>", "<username>", "<password>");
/* Connect to the MySQL test database */
con->setSchema("<database>");
stmt = con->createStatement();
res = stmt->executeQuery("<sql statement>"); // 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;
}
I got an error saying that the mysql_connection.h file was missing. I don't know what i did wrong or if there is an easier or simpler way. Please help me with this matter. Thanks.
I got an error saying that the mysql_connection.h file was missing.
Going by the error message you describe and the response to my question "Do you have the source installed for mysql_connection.h?," which was:
I didn't install mysql on the raspberry pi since I was trying to access an external database.
(Which is not what I asked.) it appears the reason #include "mysql_connection.h doesn't work is because the library isn't there.
The Connector/C library does not require the installation of MySQL, but the Connector/C source must be installed to include headers from the library, according to the documentation.
MySQL's Connector/C library requires boost; however Raspian already has boost installed, though that may come with it's own set of issue, which are discussed and resolved here if you have trouble with boost. (If your Raspberry Pi has NOOBS, I recommend switching to Raspian.)
Though the link is in my comments, instructions for installing the Connector/C library are available from MySQL's documentation.
I've collected some resources and links which should provide some additional help:
How to Install Third Party Libraries
How to #include third party libraries
Installing Connector/C++ from Source on Unix and Unix-Like Systems
I know there's a lot of suggestions of how to fix this issue, here in this forum, but none of them solved my problem.
It's always the same: code compiles fine and I can use almost all of the getXX members offered by ResultSet, except for getString, that also compiles without problem, but always crash at runtime.
I did some weird tries, like getting a char field using getInt("label") or getBlob("label"), the results weren't good as I previouslly had known, but at least I didn't experience crash.
I tried versions 1.0.5 and 1.1.3+boost. In case of 1.1.3, I also compiled from source, using CMake, but I supose that my knowlegment about the utility is not sufficient and it would last a long time before I have CMake under control. So I'd like to get a more straight solution.
My goal is to create some Win32_Console and WinForms codes, from MSVC 2010, that read and write data in MySQL 5.6 that runs in a XUbuntu machine of my network. And getString() is trapping me a lot.
Thanks in advance
I've added the code bellow:
#include "stdafx.h"
#include <iostream>
#include <mysql_driver.h>
#include <mysql_connection.h>
#include <exception.h>
#include <statement.h>
#include <resultset.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout<<"Testando uma conexao...\n";
cout<<"Connector 1.1.3:\n";
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
driver = sql::mysql::get_mysql_driver_instance();
try
{
con = driver->connect("tcp://192.168.1.75:3306", "site_rw", "");
if (con->isClosed()==0)
cout<<"Sucesso!\n\n";
cout<<"Inserindo statement...\n";
stmt = con->createStatement();
stmt->execute("USE test_area");
stmt->execute("DROP TABLE IF EXISTS test");
stmt->execute("CREATE TABLE test(id INT, label CHAR(1))");
cout<<"Criada a tabela...\n";
for (int k=0; k<30;k++)
stmt->execute("INSERT INTO test(id, label) VALUES (1, 'a')");
cout<<"Linhas inseridas...\n";
cout<<"Preparando-se para listar os inteiros:\n";
res = stmt->executeQuery("SELECT * FROM test LIMIT 0,5");
while (res->next())
cout<<"id = " << res->getInt("id") << "\n";
cout<<"\n\nAgora tentando o char(1):\n";
res->beforeFirst();
while (res->next())
cout<<"label = " << res->getString("label");
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() << endl;
cout << " (MySQL error code: " << e.getErrorCode() << endl;
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
cout<<"Terminado...\n";
return 0;
}
Aluísio
São Paulo
I'm having a bit of issues with trying to run a compiled program that uses the MySQL connector, in C++. It compiles just fine, but when running it, it'll crash immediately - seemingly so on the line that's meant to connect. I've set up all additional libraries, dependencies, pre-processors and linker inputs, and I'm using the Release solution configuration. I am running Microsoft Visual Studio 2012.
The error I'm getting is the following:
Unhandled exception at 0x6E69AF48 (msvcr90.dll) in MyLittleSQL.exe: 0xC0000005: Access violation reading location 0x00000024.
And the call stack:
MyLittleSQL.exe!main() Line 24 C++
MyLittleSQL.exe!__tmainCRTStartup() Line 536 C
Line 24 is:
con = driver->connect("tcp://127.0.0.1:3306", "sepples_su", "easy");
And the full source code is:
#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;
int main(void)
{
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", "sepples_su", "easy");
/* 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;
}
This is actually one of the examples taking from the Connector documentation, but I wanted to make sure it wasn't my own fault first.
Any help here would be appreciated, thank you.
With help from #LyubomirVasilev I compiled the C++ Connector on my own with settings for Visual Studio 11 (2012). Replacing the other lib and dll files with the compiled here, it worked perfectly after.
Information on doing this can be found here for any others: http://dev.mysql.com/doc/refman/5.1/en/connector-cpp-info.html
The latest C++ MySQL connector is compiled with VC9 runtime libraries (Visual studio 2008). Visual studio 2012 uses VC11 libraries so it's obvious why your program crashed. Your program must use the same runtime libraries as MySQL C++ connector:
Unhandled exception at 0x6E69AF48 (msvcr90.dll) <--- VC9
You must compile your program with Visual studio 2008 which uses VC9 libraries or compile MySQL C++ connector from source with Visual studio 2012.