db is not open when called the second time
-
Hi,
I have the following function:void MainWindow::Addview() { connectionNames = db.connectionNames (); if((connectionNames.contains ("qt_sql_default_connection")) == false) { db = QSqlDatabase::addDatabase ("QSQLITE"); db.setDatabaseName (fileQstring); qDebug() << "Connection Display created in connection(). "; } qDebug() << "Open connection names in Addview: " << db.connectionNames (); db.open (); if(!db.open ()) { qDebug() << "The database (db) is NOT open!"; } QSqlQuery query_main ("SELECT ID, Name,Pic, Description FROM Items ORDER BY NAME ASC "); if(query_main.isActive()==true) { qDebug() << "The query is active."; } else { qDebug() << "The query is NOT active." << query_main.lastError (); } QStandardItemModel *smodel = new QStandardItemModel(this); ui->tableView->setModel (smodel); for(int row1 = 0; query_main.next (); row1++) { if(row1 == 0) { const QSqlRecord qRec = query_main.record(); qDebug() << "The number of records: " << qRec; smodel->insertColumns (0, qRec.count()); } smodel->insertRow (row1); smodel->setData (smodel->index (row1,0),query_main.value (0).toString ()); smodel->setData (smodel->index (row1,1),query_main.value (1).toString ()); Pixmap.loadFromData (query_main.value (2).toByteArray ()); Pixmap = Pixmap.scaled (100,100,Qt::KeepAspectRatio); smodel->setData (smodel->index (row1,2),Pixmap,Qt::DecorationRole); smodel->setData (smodel->index (row1,3), query_main.value (3).toString ()); ui->tableView->setRowHeight (row1,100); } ui->tableView->horizontalHeader ()->setStyleSheet ("QHeaderView{font: 14pt Arial; color: blue; font-weight: bold;}"); ui->tableView->verticalHeader ()->setVisible (false); ui->tableView->setAlternatingRowColors (true); ui->tableView->setStyleSheet ("alternate-background-color: rgb(224,255,248); background-color: white; font: 14pt Arial"); ui->tableView->setEditTriggers (QAbstractItemView::NoEditTriggers); smodel->setHeaderData (0,Qt::Horizontal, QObject::tr ("ID")); smodel->setHeaderData (1,Qt::Horizontal, QObject::tr ("Name")); smodel->setHeaderData (2,Qt::Horizontal, QObject::tr ("Picture")); smodel->setHeaderData (3,Qt::Horizontal, QObject::tr ("Description")); ui->tableView->setColumnWidth (1,1); ui->tableView->setColumnWidth (1,200); ui->tableView->setColumnWidth (2,90); ui->tableView->setColumnWidth (3,340); ui->tableView->setWordWrap (true); }
When it is called the first time (when the program starts) it works fine. When it is called the second time from an other function I get the "The database (db) is NOT open!" error. Why is it not open the second time? Thank you.
-
Hi,
You are calling open twice in a row, that's your the current problem.
-
Hi @SGaist
I deleted the db.open() fromqDebug() << "Open connection names in Addview: " << db.connectionNames (); // db.open (); if(!db.open ()) { qDebug() << "The database (db) is NOT open!"; }
and it still says The database (db) is NOT open!
-
You should print the error you get from QSqlDatabase.
-
One of the thing you should first clean is how you handle your database. Your open statement should go in the same if where you create add the database.
Also, does
fileQstring
change at any time ? Do you handle that case ? -
In that case, why not just open the database in your main function and be done with it ?
-
No, I mean
main
in yourmain.cpp
-
@SGaist
I did the following:#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; QSqlDatabase db; QString fileQstring = "C:/Programming/Projects/FolkFriends_1_0/db.db"; db = QSqlDatabase::addDatabase ("QSQLITE"); db.setDatabaseName (fileQstring); qDebug() << "Connection Display created in connection(). "; bool OK = db.open (); if(OK == true) { qDebug() << "The db (MainWindow) is open!"; } else { qDebug() << "The db (MainWindow) is not open!"; } if(!db.open ()) { qDebug() << "The database (db) is NOT open!" << db.lastError (); } db.open(); w.show(); return a.exec(); }
I still get
The query is NOT active. QSqlError("", "Driver not loaded", "Driver not loaded") -
What version of Qt are you using ?
Do you have write access to that location ?
-
Are you experiencing that using Qt Creator to start your application ?
-
@gabor53
I think the problem is that it first executes function Addview() from mainwindow.cpp. this crates the first half of the messages:
QSqlQuery::exec: database not open
The query is NOT active. QSqlError("", "Driver not loaded", "Driver not loaded")After mainwindow.cpp it executes main.cpp where I create the db connection and open the db. This gives normal messages:
Connection Display created in main.cpp.
The db (MainWindow) is open!Clearly the problem is that it tries to run the query before it opens the db. I just still don't know why it processes mainwindow.cpp first and main.cpp second.