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
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 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.
I am just using the example code from the mysql site. This program will compile and run if I do it in debug. If I compile in release it gives me the error in the title. I downloaded all the connectors and server from oracle site, so everything is up to date on my end. I even compiled both the latest releases of c++ connectors and c connectors. I have done a lot of searches on this and tried what they said to do, but could not fix this. My lib and dll files from what I have seen have the mysql_get_option function listed. I made sure there are no stray dll or lib files on my computer. I checked the system path variable to make sure it wasnt pointing in some random area. I am using Vs2013.
Please help I have tried to fix this for a week and my brain is about to explode!! Thank you for any advice.
P.S.
I program as a hobby so it is more than likely I am overlooking something trivial.
/* Copyright 2008, 2010, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
There are special exceptions to the terms and conditions of the GPL
as it is applied to this software. View the full text of the
exception in file EXCEPTIONS-CONNECTOR-C++ in the directory of this
software distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* 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 World0000........ 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("*****", "*****", "*****");
/* Connect to the MySQL test database */
//con->setReadOnly(true);
con->setSchema(*****);
cout << "\nHere!";
stmt = con->createStatement();
res = stmt->executeQuery("SELECT DISTINCT eqdkp10_raid_attendees.raid_id, eqdkp10_raids.raid_value \
FROM eqdkp10_raids LEFT JOIN eqdkp10_raid_attendees ON eqdkp10_raids.raid_id \
= eqdkp10_raid_attendees.raid_id AND eqdkp10_raid_attendees.member_id=2");
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;
system("PAUSE");
return EXIT_SUCCESS;
}
I googled your post title and found this link: http://www.linuxtalks.net/mysql-community-server-5-7-4-m14-has-been-released/
If your search the page for mysql_get_option you will see they fixed this known bug. If you're using VS2013 your library version might include this bug since the page refers to a release in 2014.
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.