sqlite - driver not loaded
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
everything is ok but if I'm trying to use a pointer
Code:
db ->addDatabase("QSQLITE");
I get the error that the Driver is not loaded. Anyone has any idea why the second case doesn't work?
Re: sqlite - driver not loaded
Have you deployed the SQL drivers into the appropriate subdirectory of your app directory, as specified in the docs?
Re: sqlite - driver not loaded
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?
Re: sqlite - driver not loaded
The second code snippet doesn't do anything with the return value of addDatabase().
Quote:
Originally Posted by
Saurian
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,
_
Re: sqlite - driver not loaded
I don't understand what you mean. The code is a little more complex. So basically the code is something like that:
Code:
int record = 0;
//inițializări
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
//conectare la baza de date
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 ->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");
Re: sqlite - driver not loaded
Quote:
Originally Posted by
Saurian
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,
_
Re: sqlite - driver not loaded
Quote:
Originally Posted by
Saurian
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:
Good luck.