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,
    The second time you call the function, you also open "db" for the second time (without closing it before(?)). The error message may just lead you in the wrong direction.
    -Michael.



  • @m.sue
    It is supposed to update the tableview after adding a record. But that does not happen either though the signal - slot works because the processing entering the Addview().


  • Lifetime Qt Champion

    Hi,

    You are calling open twice in a row, that's your the current problem.



  • Hi @SGaist
    I deleted the db.open() from

        qDebug() << "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!


  • Lifetime Qt Champion

    You should print the error you get from QSqlDatabase.



  • @SGaist
    The error message:
    The database (db) is NOT open! QSqlError("", "Driver not loaded", "Driver not loaded")


  • Lifetime Qt Champion

    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 ?



  • @SGaist
    fileQString doesn't change. I keep using the same db.


  • Lifetime Qt Champion

    In that case, why not just open the database in your main function and be done with it ?



  • @SGaist
    You mean the main function of MainWindow_


  • Lifetime Qt Champion

    No, I mean main in your main.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")


  • Lifetime Qt Champion

    What version of Qt are you using ?

    Do you have write access to that location ?



  • @SGaist
    Yes . I have write access. Using Qt 5.7 on Windows 10


  • Lifetime Qt Champion

    Are you experiencing that using Qt Creator to start your application ?



  • @SGaist
    It starts ok.



  • @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.


  • Moderators

    @gabor53 Then move

    MainWindow w;
    

    after setting up the connection.



  • @jsulm
    This worked. Thank you.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.