I have a problem when i use mysql connector c++ and visual studio on the debug mode. It compiles just fine. My algorithm works on release mode. When i switch to debug mode, he complies but he crash while running.
I believe that he crash each time i use res->getString() .
I m using visual studio 2013, mysql connector.C++ 1.1 . I already added the dependencies
C:\Program Files\MySQL\Connector.C++ 1.1\lib\debug
C:\Program Files\Boost SDK;
C:\Program Files\MySQL\Connector.C++ 1.1\include
mysqlcppconn.lib and mysqlcppconn.dll
CPPCONN_PUBLIC_FUNC= ;HAVE_INT8_T=1 ( on the processor definition)
run time library : /MDd ( it the only think that works )
I copied mysqlcppconn.lib and mysqlcppconn.dll in the debug directory of my project
with this configuration my programm works on release mode ( changing Connector.C++ 1.1\lib\debug to Connector.C++ 1.1\lib\opt ).
In the debug mode, he says Debug Assertion failed :
File: f:\dd\vctools\crt\crtw32\misc\dbgheap.c
Line 1322
Expression : _CrtIsValidHeapPointer( pUserData)
Press Rety to debug the application
then i had to choose one buttom ( Ignore; Restart ; leave ). When i press Ignore he continue to run i have the first results but he crashs again
This is my code section
{
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <memory>
#include <string>
/* MySQL Connector/C++ specific headers */
#include <cppconn/driver.h>
#include <cppconn/connection.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/resultset.h>
#include <cppconn/metadata.h>
#include <cppconn/resultset_metadata.h>
#include <cppconn/exception.h>
#include <cppconn/warning.h>
#include "mysql_connection.h"
using namespace std;
using namespace sql;
int main(int argc, const char **argv)
{
const char* str_DataBaseMarketData_Server = "root";
const char* str_DataBaseMarketData_User = "root";
const char* str_DataBaseMarketData_Password ="root";
const char* str_DataBaseMarketData_Database ="bd test";
cout << "Connector/C++ tutorial framework..." << endl;
cout << endl;
string fld;
try {
sql::Driver* driver = get_driver_instance();
std::auto_ptr<sql::Connection> con(driver->connect(str_DataBaseMarketData_Server, str_DataBaseMarketData_User, str_DataBaseMarketData_Password));
con->setSchema(str_DataBaseMarketData_Database);
std::auto_ptr<sql::Statement> stmt(con->createStatement());
sql::SQLString *data = new SQLString();
std::auto_ptr<sql::ResultSet> res(stmt->executeQuery("SELECT * FROM news "));
//ResultSetMetaData *res_meta = res->getMetaData();
//cout << res_meta->getColumnDisplaySize( 1) << endl << endl;
while (res->next()){
fld = res->getString(2).c_str();
}
}
catch (sql::SQLException &e) {
/*
The MySQL Connector/C++ throws three different exceptions:
- sql::MethodNotImplementedException (derived from sql::SQLException)
- sql::InvalidArgumentException (derived from sql::SQLException)
- sql::SQLException (derived from std::runtime_error)
*/
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
/* Use what() (derived from std::runtime_error) to fetch the error message */
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
return EXIT_FAILURE;
}
cout << "Done." << endl;
return EXIT_SUCCESS;
}
}
This things turns me crazy because when i replace getString() by anything else, it works sweetly.Could you help me please ?
I m running under a 32 bit.
Related
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 attaching the minimal code below. The problem is with static string object that is leaking memory. I think the problem is with the string object not being initialized properly. The program runs fine in Debug mode but crashes in the Release mode.
I am using Windows 7 : 64bit - MS Visual Studio 2012
I have tried initializing the object with empty string but it did not solve the problem as suggested here
what to do if debug runs fine, but release crashes
I enabled "Treating warnings as Errors" also did not help as there are no warning as suggested by the following post
what to do if debug runs fine, but release crashes
There were some other suggestions too like "static initialization order fiasco" but I do not think its related to my issue.
Any help is appreciated
main.cpp
//main.cpp
#include "MyParameters.h"
using namespace std ;
int main( int argc, char *argv[] )
{
try
{
cout << "MyParameters::m_outputDir: " << MyParameters::m_outputDir << endl ;
bool initialized = MyParameters::initialize( "myimo.xml" ) ;
cout << "MyParameters::m_outputDir: " << MyParameters::m_outputDir << endl ;
cout << "Terminating the application..." << endl ;
}
catch ( std::exception &e )
{
cout << e.what() << std::endl;
}
}
MyParameters.h
//MyParameters.h
#ifndef __MY_PARAMETERS_H
#define __MY_PARAMETERS_H
#include <string>
#include <iostream>
#include <QString>
class MyParameters
{
public:
static std::string m_outputDir; ///< output directory
static bool initialize( const QString &xmlFile );
private:
MyParameters();
};
#endif /* __MY_PARAMETERS_H */
MyParameters.cpp
//MyParameters.cpp
#include "MyParameters.h"
#include <QDir>
std::string MyParameters::m_outputDir ;
using namespace std ;
MyParameters::MyParameters()
{
}
bool MyParameters::initialize( const QString &xmlFile )
{
m_outputDir = QDir::current().absoluteFilePath( xmlFile ).toStdString(); // --> this crashes
//m_outputDir = "C:\\Dev\\" ; // --> works fine
cout << "m_outputDir: " << m_outputDir << endl ;
cout << "myparameters.xml file reading is complete" << endl ;
return true;
}
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've stuck with a problem, trying to insert text as blob value, MySQL C++ connector crashed with exception: "Access violation reading location". I've seen questions like this here, but none has been answered. Here is a code sample:
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
#include <string>
#include <iostream>
#include <sstream>
using namespace sql;
void main(){
Connection *dbConnection;
Driver *driver;
Connection *con;
driver = get_driver_instance();
con = driver->connect("localhost", "root", "");
Statement *stmt = con->createStatement();
try{
stmt->execute("USE test");
stmt->execute("DROP TABLE blob_test");
stmt->execute("CREATE TABLE blob_test("
"id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,"
"original BLOB NOT NULL);");
char smallBlob[32];
for (char i = 0; i < sizeof(smallBlob); ++i)
{
smallBlob[i] = i;
}
std::istringstream str(smallBlob);
PreparedStatement *pstmt = con->prepareStatement("INSERT INTO blob_test(id, original) VALUES (1, ?)");
pstmt->setBlob( 1, &str);
pstmt->executeUpdate();
}catch(sql::SQLException &e){
std::cerr << "# ERR: " << e.what();
std::cerr << " (MySQL error code: " << e.getErrorCode();
std::cerr << ", SQLState: " << e.getSQLState() << " )" << std::endl;
}
}
Thanks in advance!
Problem solved compiling MySQL cpp connector from sources.
If you go that way read carefully this and this (specially comments in the end). I also had to changed a couple of lines in config.h to implicit cast of int to char*. Also be attentive don't mix Debug and Release, for example if you compile your application in Debug, you should link Debug version of MySQL connector and vice-a-verse.
After I linked lib that I had compiled everything launched )
You may read on dev.mysql.com forums that in most cases it's the only way force the connector to work. While compiling I met with the thing that prove this, include files in source code is different from includes in windows binary distributive.
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.