[Solved]Error: Driver not loaded Driver not loaded on Ubuntu
-
While running the next to files I'll get this run-time error:
Database error: Driver not loaded Driver not loadedI don't see why this isn't working since i am creating an instance of the db variable in the header and declaring it later on.
I'm pretty new to Qt and C++ so probably i am just missing something.
I hope someone can help me with this problem.
databaseConnector.h
@#ifndef DATABASECONNECTOR
#define DATABASECONNECTOR#include <QSqlDatabase>
class databaseConnector
{
private:
QSqlDatabase *db;public:
databaseConnector();
bool connectDb();
bool disconnectDb();
};#endif // DATABASECONNECTOR_H@
databaseConnector.cpp
@#include "databaseconnector.h"
#include <QMessageBox>
#include <QObject>
#include <stdio.h>
#include <QSqlError>databaseConnector::databaseConnector() {
db = new QSqlDatabase();
db->addDatabase("QMYSQL");
db->setHostName("127.0.0.1");
db->setDatabaseName("dbname");
db->setUserName("root");
db->setPassword("root");
}bool databaseConnector::connectDb() {
if(!db->open()) {
QMessageBox::critical(0, QObject::tr("Databse Error"), db->lastError().text());
return false;
}
return true;
}bool databaseConnector::disconnectDb() {
return true;
}@main.cpp
@
#include <QtGui>
#include <QApplication>
#include <stdio.h>
#include #databaseconnector.h"int main(int argc, char *argv[])
{
databaseConnector *dbconnector = new databaseConnector();if(dbconnector->connectDb()){
printf("Connection failed");
}
printf("Connection worked");
}@ -
Your code compiles, so there is no real error in that.
It's very likely that the mysql plugin for Qt is not found.
Some points to start reading:
- General instructions on "Deploying Qt Applications":http://doc.qt.nokia.com/4.7/deployment.html
- "Deploying an Application on X11 Platforms ":http://doc.trolltech.com/4.7/deployment-x11.html
If change your main method to the following, what does the console output say?
@
#include <QtGui>
#include <QApplication>
#include <QDebug>
#include <stdio.h>
#include "databaseconnector.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);qDebug() << qApp->libraryPaths();
databaseConnector *dbconnector = new databaseConnector();
if(dbconnector->connectDb()){
printf("Connection failed");
}printf("Connection worked");
}
@Is the mysql plugin listed in a subdir "sqldrivers" in one of the paths printed?
-
Thank you for your reply.
the code does compile so I know that it isn't a wrong syntax. But it gives the error during runtime.
When I change the code in main(), and follow the paths, I have a folder sqldrivers in one of those paths. In that folder there is a file called libqsqlmysql.so. I suppose that the mysql driver is installed and works correctly.
The thing is that when I change only a small part of my code it works, but then I can't declare the variable db global (in the header file).
The things i changed:- remove the private: part in the header file
- Place the next code in the function databaseConnector::connectDb()
@
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("127.0.0.1");
db.setDatabaseName("Nedtrain");
db.setUserName("root");
db.setPassword("root");
if(!db.open()) {
QMessageBox::critical(0, QObject::tr("Databse Error"), db.lastError().text());
return false;
}
return true;
@
and clear all the code in the constructor.
Thanks again for any reply's
-
Ok, that's clear. The db object is instantiated at the wrong time (no library paths setup, for example) and so fails to load the plugin.
You should try hard to avoid global variables.
Oh, and you can retrieve the db object any time later, no need to save it in a global variable.
-
So correct me if I am wrong;
Because I use a QSqlDatabase db in the header, the paths aren't instantiated yet. So I can't use the db object later on. So when I want to close my database in an new function I can't just use the db object declared in the header (because I get the error then), or can I? Or should I use the next code part to close the database?
@
void databaseConnector::disconnectDb() {
QSqlDatabase db = QSqlDatabase::database();
db.close();
db.removeDatabase("QMYSQL");
}
@ -
If you declare the variable in the header as global, it is up to the compiler, when exactly the object is created (and that varies from compiler to compiler!), so it can be that there is an attempt to create the object at a time, when Qt is not finally set up yet - which can lead to not finding plugins and the like.
The steps are basically:
- create a database connection somewhere in a connectDb method or the like:
@
QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
db.setHostName("acidalia");
db.setDatabaseName("customdb");
db.setUserName("mojito");
db.setPassword("J0a1m8");
bool ok = db.open();
@
The above snippet creates a default database; if you need more DBs, add a second parameter to the addDatabase call with the internal name you want to identify it with.- get the database handle wherever you need it
@
QSqlDatabase db = QSqlDatabase::database();
@
This retrieves the previously setup default database; add a name to it if you have multiple ones.- close the database in a disconnectDB method
Get the handle like above and call close on it.
This way you get rid of the global object and hence avoid initialization hassles caused by the linker.
Oh, and an important note:
"QSqlDatabase::addDatabase() ":http://doc.qt.nokia.com/4.7/qsqldatabase.html#addDatabase, you must not call it on an QSqlDatabase object created with new! Just do it like the samples in the docs (= the snippets I copied above) show you; you just need to change the driver name, username, password, etc.[Code snippets copied from the "QSqlDatabase documentation":http://doc.qt.nokia.com/4.7/qsqldatabase.html of Qt]
-
[quote author="Volker" date="1304607729"]@ ...
qDebug() << qApp->libraryPaths();
...@Is the mysql plugin listed in a subdir "sqldrivers" in one of the paths printed?[/quote]
Thanks for those accurate advices Volker, I was wondering why the driver wasn't found when I past the .so next to my executable. I was missing to put it in a subfolder named sqldrivers !