Hey guys,
I currently have an issue with QtSql/QSqlDatabase libraries with Qt C++ on Debian with Qt Creator 5.8.2 (Qt 5.11.3 GCC 32 bit); my latest industrial software engineering project requires multiple MySQL database connections on different threads. After some research, it seems that I need to name my database connections. However whenever I add a second argument to QSqlDatabase::addDatabase, the drivers become unavailable.
The following code works fine and I can verify on the SQL CLI that the data is being received correctly:
db.setHostName("00.00.000.000");
db.setDatabaseName("00000");
db.setUserName("00000");
db.setPassword("00000");
bool dbOk = db.open();
if(dbOk)
{
qDebug() << "Connected to Database.";
queryData.sprintf("INSERT INTO `dev`.`logs` (`logid`, `timestamp`, `command`) VALUES (NULL, CURRENT_TIMESTAMP, 'Test')");
qDebug() << "Output Query Data: " << queryData << ".";
if(!query.isActive())
{
qDebug() << "ERROR Error with database query! " << "Last Database Error: " << db.lastError() << ". Last Query Error: " << query.lastError() << ".";
}
else
{
qDebug() << "Query is good!";
}
}
connection = db.connectionName();
db.close();
db.removeDatabase(connection);
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("00.00.000.000");
db.setDatabaseName("00000");
db.setUserName("00000");
db.setPassword("00000");
bool dbOk = db.open();
if(dbOk)
{
qDebug() << "Connected to Database.";
QString queryData;
queryData.sprintf("INSERT INTO `dev`.`logs` (`logid`, `timestamp`, `command`) VALUES (NULL, CURRENT_TIMESTAMP, 'Test')");
qDebug() << "Output Query Data: " << queryData << ".";
QSqlQuery query(queryData);
if(!query.isActive())
{
qDebug() << "ERROR Error with database query! " << "Last Database Error: " << db.lastError() << ". Last Query Error: " << query.lastError() << ".";
}
else
{
qDebug() << "Query is good!";
}
}
QString connection;
connection = db.connectionName();
db.close();
db = QSqlDatabase();
db.removeDatabase(connection);
To copy to clipboard, switch view to plain text mode
But when I change:
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
To copy to clipboard, switch view to plain text mode
To:
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "MyConnName");
To copy to clipboard, switch view to plain text mode
I get "ERROR Error with database query! Last Database Error: QSqlError("", "", "") . Last Query Error: QSqlError("", "Driver not loaded", "Driver not loaded") .". Does anyone have any idea as to why this is happening?
Any help would be greatly appreciated!
Thanks,
Ryan
Bookmarks