PDA

View Full Version : sqlite - driver not loaded



Saurian
2nd July 2015, 19:32
Hello,

I'm using Qt to work with a sqlite database but I have an error. If I try to declare the handler for the QSqlDatabase object as a pointer I get the error: Driver not loaded. So if I use



QSqlDatabase db;
db = QSqlDatabase::addDatabase("QSQLITE");


everything is ok but if I'm trying to use a pointer



QSqlDatabase *db;
db = new QSqlDatabase;
db ->addDatabase("QSQLITE");


I get the error that the Driver is not loaded. Anyone has any idea why the second case doesn't work?

d_stranz
2nd July 2015, 19:38
Have you deployed the SQL drivers into the appropriate subdirectory of your app directory, as specified in the docs?

Saurian
2nd July 2015, 19:43
Well, I have no problem with the SQL driver for the first case (when I don't use a pointer) so I haven't changed the location for the SQL driver. But because I use a pointer you say I might need to have the dll file in the folder of my application?

anda_skoa
2nd July 2015, 19:50
The second code snippet doesn't do anything with the return value of addDatabase().


Well, I have no problem with the SQL driver for the first case (when I don't use a pointer)
The first case also uses the initialized database connection returned by addDatabase() and stores it in the local variable.

Cheers,
_

Saurian
2nd July 2015, 20:11
I don't understand what you mean. The code is a little more complex. So basically the code is something like that:



QSqlDatabase db;
int record = 0;

//inițializări
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
QSqlQuery *query;
QMessageBox *messageBox = new QMessageBox;

ui->setupUi(this);


//conectare la baza de date
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("conturi.db");
if (!db.open())
{
messageBox ->warning(this, tr("Database Error"), db.lastError().text());
exit(0);
}
else
{
//adăugare listă conturi din baza de date
query = new QSqlQuery(db);
query ->exec("SELECT * FROM cont");
if (!query ->isActive())
{
messageBox ->warning(this, tr("Database Error"), query ->lastError().text());
exit(0);
}
else
{
while (query ->next())
{
ui ->comboBox ->addItem(query ->value(3).toString());
}
}
}
}

//curățare tot
MainWindow::~MainWindow()
{
db.close();
delete ui;
}


And of course there are other line codes for other operations on the database. So what I changed was defining db as a pointer and made changes everywhere from "." to "->" and from "db" to "*db".

Later edit: So using the method addDatabase I didn't assigned the driver QSQLITE to the variable db? I must use another function or something like that?

Added after 15 minutes:

Ok, so I found which was the solution: *db = QSqlDatabase::addDatabase("QSQLITE");

anda_skoa
2nd July 2015, 21:04
So using the method addDatabase I didn't assigned the driver QSQLITE to the variable db?
If you are still wondering, think about how the method could possibly be able to do that.
It does know nothing about "db" and prior to your fix you discarded its return value.

Cheers,
_

jefftee
3rd July 2015, 02:29
I get the error that the Driver is not loaded. Anyone has any idea why the second case doesn't work?
To elaborate on what @anda_skoa said:

QSqlDatabase::addDatabase is a static method and you are not operating on the db instance you think you are, because static member methods don't have a "this" pointer and thus do not operate on an instance of an object.

QSqlDatabase::addDatabase returns a QSqlDatabase instance, but you're not assigning that to a variable, so your attempt to add the database connection is essentially a no-op. You are creating a database instance by calling QSqlDatabase::addDatabase, then you throw away the result, hence the no-op.

The instance you allocated via "new QSqlDatabase()" is still an invalid database object since you have only invoked its default constructor.

If you *must* create a QSqlDatabase instance on the heap, try something like this:



QSqlDatabase *db = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE"));


Good luck.