Mysql Connector connect() issues - c++

I have a small C++ project and need to access a mySQL DB from it, so i have setup mySQL Connector for C++.
This is done on OS X 10.10, and i got no problems with the compilation/linking.
I have written a class for all the mysql stuff, and in the constructor i want to setup the connection to the db. However, this seems to be kinda hard.
Here is the relevant part from the class:
class mysql{
public:
mysql(std::string server, std::string user, std::string password);
private:
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;
std::string last_error = "";
};
And here the implementation:
mysql::mysql(std::string server, std::string user, std::string password){
driver = sql::mysql::get_mysql_driver_instance();
try{
con = driver->connect(server, user, password);
last_error = "";
}
catch(sql::SQLException &e){
last_error = e.what();
}
}
However, when i create an object of that class like this:
mysql db("tcp://127.0.0.1:3306", "root", "secretsecret");
I then have this in my last_error string:
Unknown MySQL server host '???' (0)
The "host" sometimes differs even tho i dont change it in code. This seems like internally a different memory location is read out as it should be.
But even if i pass the connect() variables directly when i call it, i get this error. Same when saving those three variables internally in the mysql class and use those to call connect().
Anyone has an idea what could cause this? I have a similar implementation in a different project where this does work fine so im kinda confused :/

Here is a post that matches to your circumstances (The C++ connector works on linux and fails on OSX).
With using mysql logging/tracing or running it in debugger, you may be able to gather more information to report to mysql developers. You may have better luck.

After a long time of googeling i found this the most useful link: https://apple.stackexchange.com/questions/251290/weird-behaviour-of-mysql-connector-c-in-osx
Following the hints there, i recompiled the myscl c connector and then the mysql c++ connector (version 1.1.6 cause 1.1.7 caused a json error while compiling).
I also saw in the cmake logs of the c++ connector that Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ was used to compile the library.
So i used that one too for my compilation. It could be that this would work, tho im also including wxwisgeds in my project resulting in this error:
/usr/local/include/wx-3.0/wx/strvararg.h:30:18: fatal error: 'tr1/type_traits' file not found
#include <tr1/type_traits>
I found some hints for that one but they all just produced a massiv amount of new errors, so i stopped digging deeper here.
My best options would be to create two seperate programs, on to communicate with mysql and one to provide the gui and let them both communicate with each other either via sockets or files providing a very inefficient access to a mysql db.
Good luck to anyone who runs into the same thing..

Related

Proper error handling for OCCI C++ library

I want to understand how should I correctly handle exceptions in OCCI C++ library (https://docs.oracle.com/database/121/LNCPP/relational.htm#LNCPP003).
Below is simple example of creating connection, statement and executing statement.
Environment *env = Environment::createEnvironment();
Connection *conn = env->createConnection(
userName, password, connectString);
Statement *stmt = conn->createStatement(
"SELECT blobcol FROM mytable");
try {
stmt->execute();
} catch (oracle::occi::SQLException &ex) {
// what to do here?
}
My questions are:
Is there a standardized way to handle occi::SQLException? In perfect world I want to know on which exceptions I can safely just retry my stmt->execute and have a possibility to get successful result. I figured out that occi::SQLException has a member is SQLException::isRecoverable(), does it mean I can just retry on all these recoverables errors without reconnect etc.? Is there a documented list of this recoverable errors?
How to know if connection lost? Is there a known list of sql codes for lost connection case?
If connection lost what is the correct way to recover? Should I get rid of old Connection object and create new connection with env->createConnection and hence I have to recreate all statements using new connection? Or maybe I should just retry executing my statements and connection with automatically recover inside OCCI driver library?

SignalR-Client-Cpp on Petalinux Throwing "Error in SSL handshake" Exception

I have a Xilinx ZCU106 with a Petalinux build I created that includes an application using SignalR-Client-Cpp. Despite trying a number of things, I'm continually getting an "Error in SSL handshake" exception after calling start() on my signalr::hub_connection.
This application runs fine on other Linux systems like Ubuntu. I think the problem is it's having trouble finding the ca-certificates.crt file which is usually in /usr/local/ssl on more normal Linux distro's like Ubuntu. In the Petalinux build it's located here: /etc/ssl/certs/ca-certificates.crt.
The best I can tell, I need to do something like this to configure the signalr::hub_connection to use the certificate file:
web::http::client::http_client_config httpConfig;
httpConfig.set_ssl_context_callback([](boost::asio::ssl::context& ctx) {
ctx.load_verify_file("/etc/ssl/certs/ca-certificates.crt"); });
web::websockets::client::websocket_client_config wsConfig;
wsConfig.set_ssl_context_callback([](boost::asio::ssl::context& ctx) {
ctx.load_verify_file("/etc/ssl/certs/ca-certificates.crt"); });
signalr::signalr_client_config signalrConfig;
signalrConfig.set_http_client_config(httpConfig);
signalrConfig.set_websocket_client_config(wsConfig);
auto hubConn = signalr::hub_connection_builder::create(signalrURI).build();
hubConn.set_client_config(signalrConfig);
std::promise<void> task;
hubConn.start([&task](std::exception_ptr exception) {
// Code that checks the exception, etc
Yet, even when doing this, the exception that is passed into start() is populated, stating "Error in SSL handshake".
I've tried some other things like using web::credentials and setting those on the signalr_client_config before giving it to the hub_connection but I get the same results.
I'm out of ideas as to how to get this to work and I'm hoping someone else might have some ideas?

Connecting to a local database using mysql-cpp-connector

Introduction
I am new to using mysql in c++ and have been trying to use mysqlcppconnector for connecting to my local database. I have successfully linked the libraries after a lot of effort. (It wasn't straightforward for me)
Preparation
I am using the following code to connect with a database running on my local.
#include <mysqlx/xdevapi.h>
void make_db_connection() {
mysqlx::Session sess("localhost", 33060, "mudrex", "mudrex");
mysqlx::Schema db= sess.getSchema("mudrex");
// or Schema db(sess, "test");
mysqlx::Collection myColl = db.getCollection("user_api_key_map");
// or Collection myColl(db, "my_collection");
mysqlx::DocResult myDocs = myColl.find("user_id")
.limit(1)
.bind("user_id","L%").execute();
cout << myDocs.fetchOne()<<endl;
}
Errors
When I call the above function, I get the following error :
libc++abi.dylib: terminating with uncaught exception of type mysqlx::abi2::r0::Error: CDK Error: Connection refused (generic:61)
Abort trap: 6
Any help would be really appreciated. Thank you for your time.
mysqlx::Session is for connection to the MySQL X protocol. The Error: Connection refused is the very first hint of missing listeners of the port 33060. You need to install and enable the MySQL Shell that supports X Protocol and enables you to use X DevAPI. See https://dev.mysql.com/doc/mysql-shell/en/mysql-shell-install.html for instructions.

QtSql connection

I am trying to create a connection to a database and insert/delete/make queries to the database. I know SQL relatively well but I cannot seem to wrap my head around it in Qt. I used to program in Delphi.
This is my code so far:
QSqlDatabase db;
db.addDatabase("QSQLITE");
db.setHostName( "localhost" ); //I don't know if i should include this the database is in the same directory as my program
db.setDatabaseName( "Xmato.odb" );
db.setUserName( "" ); //There is no username
db.setPassword( "" ); //There is no password
db.open();
db.prepare("SELECT * FROM Members");
db.exec();
I have added this to my .pro file:
QT += sql;
An included QtSql to my main file.
When I run this code I get the error:
QSqlQuery::prepare: database not open
Any ideas will me much appreciated.
P.S.: I use c++ on Linux Ubuntu 12.04 and used LibreOffice Base to create my database.
After a bit of google-ing - openoffice libre's internal database is using HSQLDB (natural choice for Java). Here's a small discussion about HSQLDB.
It appears that some versions of openlibre base is also able to connect to external databases. I would recommend setting up something that is more accessible to C++, specifically Qt.
Only a few drivers like ODBC & SQLite is included by default.
Which means that depending on the database being used, one may need to get additional source code (or packages) and compile a plugin/dll/so. The library is loaded dynamically (i.e. run-time) by the QtSql module. I've run into this for mysql drivers.
When you get all of that setup, your call to addDatabase should match the kind of database you're using.
QSqlDatabase::addDatabase( "QODBC" ); // For something like MSSQL
QSqlDatabase::addDatabase( "QSQLITE" ); // For SQLite
QSqlDatabase::addDatabase( "QMYSQL" ); // For MySQL
Personally, if you're just doing this for kicks, a quick and easy database is SQLITE. You can even download plugins/extensions for Mozilla Firefox that will offer you a GUI to the database.
Driver not loaded
you need the QSQLITE driver.
db.drivers() returns a list of all the available database drivers.
In Ubuntu 12.04 the driver for sqlite is in a package named libqt4-sql-sqlite.
But: is odb a sqlite database??
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName( "Xmato.odb" );
if (db.open())
{
QSqlQuery query(db); // if multiple connections used, without the `db` in constructor will cause the query to use the default database (first opened and available one)
query.exec("select * from members");
}
should do the same. username and password are not needed and since it is a file, you just have to use setDatabaseName to set what file you want to open.

disconnecting from mongoDb with C++ driver

i'm sure this must be really simple or i'm missing the point, but how do you disconnect from Mongo using the C++ driver and DBClientConnection? DBClient has a public member of 'connect' but no disconnect/kill/drop etc that I can find.
There is some talk (in stack overflow and on the web) of using ScopedDBConnection which does seem to be able to allow me to drop my connection - but there are very few examples of how it would be used - or info on when I should use that class over the DBClientConnection class.
Any ideas?
If you're using a DBClientConnection, it has one connection, and you aren't supposed to disconnect/reconnect. I guess it kills the connection when it calls the destructors. You can set it up to automatically reconnect so you can keep using it if it loses its connection.
If you want to have connection pooling and multiple connections, you want to use ScopedDBConnection. You can see some examples here: https://github.com/mongodb/mongo/blob/master/src/mongo/client/model.cpp
Here's the gist:
ScopedDbConnection conn("localhost");
mongo::BSONObjBuilder obj;
obj.append( "name" , "asd" );
conn->insert("test.test", obj);
conn.done();
Basically, you can do anything with conn that you can do with a DBClientConnection, but when you're done you call done().