Crashes when running MySQL Connector/C++ from Visual Studio - c++

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.

Related

MySQL connector/C++ error "Unresolved external symbol _get_driver_instance" in Visual Studio 2017

I know this topic is common on this forum, but after all researching for solution I still cannot make it work.
I did everything from Developer Guide:
Linked Boost libraries
Linked Connector/C++ static library
Switched from Debug to Release
Installed binary (I really don't want to build from source unless I absolutly have to)
When I am trying to build an example code, which looks like this:
/* 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!' 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;
}
Output looks like this:
1>------ Build started: Project: MySQL test, Configuration: Release Win32 ------
1>MySQL test.cpp
1>Info: Boost.Config is older than your compiler version - probably nothing bad will happen - but you may wish to look for an update Boost version. Define BOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE to suppress this message.
1>MySQL test.obj : error LNK2001: unresolved external symbol _get_driver_instance
1>C:\Users\rafal\source\repos\MySQL test\Release\MySQL test.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "MySQL test.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Boost libraries version is 1_66_0, that's the latest one.
Configuration Properties / C/C++ / General / Additional Include Directories: C:\boost; C:\Program Files\MySQL\MySQL Connector C++ 1.1.9\include
Configuration Properties / C/C++ / Preprocessor / Preprocessor Definicions: WIN32l; NDEBUG; _CONSOLE; CPPCONN_PUBLIC_FUNC=
Configuration Properties / Linker / General / Additional Library Directories: C:\Program Files\MySQL\MySQL Connector C++ 1.1.9\lib\opt
Configuration Properties / Linker / Input / Additional Dependencies: mysqlcppconn-static.lib
Configuration Properties / Linker / Command Line:
Debug
Release
What can I do to not get this error?
Installed binary (I really don't want to build from source unless I absolutly have to)
You're gonna absolutely have to.
There are no official binaries for Visual studio 2017 (VC 15) at the moment of writing of this answer. As I've checked in official downloads, binaries are for Visual studio 2013 - VC 12.

C++ MySQL program crashes with error 255 on Windows

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

How to establish connection with database using MySQLConnectorC++?

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.

Connecting the Raspberry Pi 2 with an External Remote Database

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

/gs option causing program to throw exception

Recently, I've been trying to make a little test program to communicate with a MySQL server in C++. I'm currently using MySQL Connector/C++ as my API to connect to my database server. It took me a very long time to get it running, because oracle/mysql has little to no documentation of how to use connector/c++ with Visual Studio 10+.
Finally after getting everything working, there seems to be some issue when the application tries to exit. It throws the following unhandled exception:
Unhandled exception at 0x00C62291 in mysql2.exe: Stack cookie instrumentation code detected a stack-based buffer overrun.
After researching about the error, I figured out it was due to the "security check" option (/gs compiler option). When I disable this compiler option, the application exits gracefully.
I have a feeling that I should not turn it off, as it is the default option in Visual Studio 2012 (and possibly other versions?)
My questions are:
Why is this /gs compiler option causing the unhandled exception?
Is it safe or okay to turn off the /gs compiler option?
Here is the piece of code the unhandled exception points to (inside a file, called: gs_report.c):
#if defined (_M_IX86) || defined (_M_X64)
if (IsProcessorFeaturePresent(PF_FASTFAIL_AVAILABLE))
#endif /* defined (_M_IX86) || defined (_M_X64) */
__fastfail(FAST_FAIL_STACK_COOKIE_CHECK_FAILURE);
Here is my application code:
/* 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 "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 main(void)
{
cout << endl;
cout << "Let's have MySQL count from 10 to 1..." << endl;
try {
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
sql::PreparedStatement *pstmt;
/* Create a connection */
driver = sql::mysql::get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "MSxa5y");
/* Connect to the MySQL test database */
con->setSchema("test");
stmt = con->createStatement();
stmt->execute("DROP TABLE IF EXISTS test");
stmt->execute("CREATE TABLE test(id INT)");
delete stmt;
/* '?' is the supported placeholder syntax */
pstmt = con->prepareStatement("INSERT INTO test(id) VALUES (?)");
for (int i = 1; i <= 10; i++) {
pstmt->setInt(1, i);
pstmt->executeUpdate();
}
delete pstmt;
/* Select in ascending order */
pstmt = con->prepareStatement("SELECT id FROM test ORDER BY id ASC");
res = pstmt->executeQuery();
/* Fetch in reverse = descending order! */
res->afterLast();
while (res->previous())
cout << "\t... MySQL counts: " << res->getInt("id") << endl;
delete res;
delete pstmt;
delete con;
} catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "() 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 /gs option is allowing the runtime to detect an error in your program. Removing the option allows the program to exit without detecting it, but the error is still there. Somewhere in your code or the libraries, memory on the stack is being overwritten. I'd try a binary search on the code to see which statement is leading to the error. You may also want to always check the return value from all of your database calls.
You definitely should not disable this option. Doing so will hide real errors and may leave your program open to hacking.
Make sure you check the result codes of your method calls. This sounds similar to this question: Buffer Overrun using Mysql Connector c++ as Visual Studio's /gs flag is warning about buffer overflows.