Segmentation fault in stmt->executeQuery()
Posted by: Naveen TS
Date: December 16, 2010 06:47AM

I wrote a sample C++ program to connect to MYSQL using NetBeans IDE and MySQL Connector C++. It is throwing a Segmentation Fault. Below is the code:

#include <stdlib.h>
#include <iostream>

#include "mysql_connection.h"
#include "mysql_driver.h"

#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>

using namespace std;
using namespace sql;

int main(void)
{
try {
Driver *driver;
Connection *con;
Statement *stmt;
ResultSet *res;


/* Create a connection */
driver = get_driver_instance();

con = driver->connect("tcp://localhost:3306/ocr", "cppuser", "cpppass");

cout<<"Driver name "<<driver->getName()<<endl;
cout<<con->getClientInfo()<<"\n";
//con->setAutoCommit(0);

stmt = con->createStatement();
cout<<"Created conn.\n";


res = stmt->executeQuery("SELECT * from data");

while (res->next()) {
cout << res->getString("id") << endl;
}

delete res;
delete stmt;
delete con;

} catch (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 output is :

Running 'SELECT * from data'...
Driver name MySQL Connector C++ (libmysql)
ocr
Created conn.
ocr
/home/naveen/netbeans-6.9.1/ide/bin/nativeexecution/dorun.sh: line 33: 23006 Segmentation fault (core dumped) sh "${SHFILE}"
Press [Enter] to close the terminal ...

I've added libmysqlcppconn.so.5 (/usr/lib64/) file to my linker although another file libmysqlcppconn.so.1 also exists in the same folder.

I'm using a 64 bit Fedora 14 OS. Can this issue be in any ways related to some 32 bit Vs 64 bit compatibility issue?

Please help if anyone has any idea about this error.

Note: If i put con->setAutoCommit(0) or any setter statement for con object above the executeQuery() statement, I am getting a different error:

# ERR: SQLException in main.cpp(main) on line 80
# ERR: MySQL_Connection::prepareStatement(const sql::SQLString& sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) (MySQL error code: 0, SQLState: )

MySQL error code is mentioned as 0. :( I've googled and couldn't find anything about this particular error code.

Options: ReplyQuote


Subject
Views
Written By
Posted
Segmentation fault in stmt->executeQuery()
1650
December 16, 2010 06:47AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.