PDA

View Full Version : [SOLVED] database opened .. database not open



kapitanluffy
23rd February 2011, 21:03
so i was one day programming using Qt
i tried creating a class file name 'database' and added 3 methods
'createConnection', 'execute' and 'close'

in my main.cpp


#include <QtGui/QApplication>
#include "mainwindow.h"
#include "../library/database.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
database myDb;
myDb.createConnection("QSQLITE","../healthdex/resources/recipes.sqlite");
myDb.execute("SELECT * FROM recipe");
myDb.close();


MainWindow w;
w.show();

return a.exec();
}


for the first try it worked! then after clicking the debug button again
it returned errors


Database Opened
QSqlQuery::exec: database not open
There is an error QSqlError(-1, "Driver not loaded", "Driver not loaded")

i included the files in my project so you can see the 'database' class that i created
5981

Added after 27 minutes:

tried adding ..


if(database::querier.exec(query, this->db)){
and

if(database::querier.exec(query, database::db)){

in line 26 but it returned 'no matching function for call...'

fixed it by adding


database::querier = QSqlQuery::QSqlQuery(database::db);

above the



if(database::querier.exec(query)){

:)

wysota
27th February 2011, 11:39
One needs to use QSqlDatabase::addDatabase() to load the database driver first.