I am currently learning C++ which isn't going overly smoothly. I am doing a test with a small, simple app, connecting to a MySQL Database.
Below is my main method
int main()
{
cout << "Hello World" << endl;
DBConnectionManager dbConnManager;
dbConnManager.performSql();
return 0;
}
Below is my DBConnectionManager header file
#ifndef MYSQLTEST_DBCONNECTIONMANAGER_H
#define MYSQLTEST_DBCONNECTIONMANAGER_H
#include <stdlib.h>
#include <exception.h>
#include <resultset.h>
#include <statement.h>
#include <mysql_driver.h>
#include <mysql_connection.h>
using namespace sql;
class DBConnectionManager
{
private:
sql::Driver *driver = NULL;
sql::Connection *conn = NULL;
sql::Statement *statement = NULL;
sql::ResultSet *res = NULL;
public:
DBConnectionManager();
void performSql();
};
#endif //MYSQLTEST_DBCONNECTIONMANAGER_H
Below is my DBConnectionManager cpp constructor
#include <iostream>
#include "DBConnectionManager.h"
using namespace std;
DBConnectionManager::DBConnectionManager() {
cout << "Starting DBConnectionManager - Updated" << endl;
try {
cout << "Getting driver instance" << endl;
driver = get_driver_instance();
cout << "Got driver instance" << endl;
conn = driver->connect("tcp://127.0.0.1:3306", "root", "password");
conn->setSchema("bugs");
cout << "Connected to database" << endl;
}
catch (SQLException ex) {
cout << "Error connecting to DB: " << ex.what() << endl;
}
catch (exception ex) {
cout << "Something has gone wrong: " << ex.what() << endl;
}
}
Below is the output of my program
Hello World
Starting DBConnectionManager - Updated
Getting Driver Instance
Got Driver Instance
Something has gone wrong: bad allocation
I'm getting somewhere. I thought I needed to have CPPCONN_PUBLIC_FUNC=;mysqlcppconn_EXPORT
set in the preprocessor
Now it seems to successfully connect so now when I try and read from the database it throws an error
Now I run a performQuery function in my DBConnectionManager which is as follows
void DBConnectionManager::performSql() {
cout << "Going to perform query" << endl;
try {
if (conn == NULL)
{
cout << "conn is null" << endl;
return;
}
statement = conn->createStatement();
res = statement->executeQuery("SELECT * FROM reports");
while (res->next())
{
string value = res->getString("Summary");
cout << "ID: " << value << endl;
cout << "-------------" << endl;
break;
}
cout << "Finished Loop" << endl;
delete res;
delete statement;
delete conn;
}
catch (SQLException ex) {
cout << "Error executing query: " << ex.what() << "Error Code: " << ex.getErrorCode() << endl;
}
catch (exception ex) {
cout << "Something unexpected went wrong in performSql:" << ex.what() << endl;
}
When it runs res->getString("Summary")
it doesn't throw the exception handler but Visual Studio pops up with the error:
Exception thrown at 0x00007FFEEC421C80 (vcruntime140d.dll) in TestMySQL.exe:
0xC0000005: Access violating reading location 0xFFFFFFFFFFFF`
Resolved finally.
I first of all thought that I needed CPPCONN_PUBLIC_FUNC=;mysqlcppconn_EXPORT
to be set in the preprocessor. Turns out I don't which is why the get_driver_instance was failing.
The query issue calling res->getString() was crashing as you need to call c_str(). E.g.
string value = res->getString("Summary").s_ctr()
User contributions licensed under CC BY-SA 3.0