Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Multiple DB SQLITE



  • Hello,

    I'm on QT 5.15 / Windows and I'm not success to open 2 dataBase even folowing documentation, could you help me?

    .ccp:

    databaseManager::databaseManager()
    {
        db =  QSqlDatabase::addDatabase("QSQLITE","first_connection");
        databaseName="./Databases/db1.sqlite";
        db.setDatabaseName(databaseName);
    }
    

    When I'm calling functions, the dbInitBatteryData() below is working but the other one is blocked with message:

    QSqlDatabasePrivate::removeDatabase: connection 'first_connection' is still in use, all queries will cease to work.
    QSqlQuery::exec: database not open
    query.exec() false

    but not other query or db opened is called any where...

    void databaseManager::dbInitBatteryData()
    {
    
        {
            QSqlQuery query(QSqlDatabase::database("first_connection"));
            query.exec("CREATE TABLE IF NOT EXISTS battery_data (partnumber TEXT, serialnumber TEXT, drawing TEXT)");
            query.finish();
            query.clear();
            db.close();
        }
        QSqlDatabase::removeDatabase("first_connection");
    }
    
    void databaseManager::dbInitCheckStep()
    {
        {
            QSqlQuery query(QSqlDatabase::database("first_connection"));
            qDebug()<<"query.exec()"<< query.exec("CREATE TABLE IF NOT EXISTS check_data (step TEXT, periodical_CheckOk TEXT, periodical_CheckNo TEXT)");
            query.finish();
            query.clear();
            db.close();
        }
    
        QSqlDatabase::removeDatabase("first_connection");
    }
    

    Could you help me to understand what I did wrong because doing the same without connection name is working fine... but I need to use connection name to use an other db on my project?...

    Thank you very much


  • Qt Champions 2019

    @philxxx609 Don't use same connection name ("first_connection") - use unique connection names for each connection.
    Also do not store the connection in this "db" variable.



  • @philxxx609 said in Multiple DB SQLITE:
    your dbInitBatteryData closes the db and removes it from the list of database-connections.

    In dbInitCheckStep you try to use this closed and removed connection named "first_connection", so you get what you programmed: an invalid connection for the query which results in the given error message.

    As usual open your connection on start, close and remove it at end, let it open during the runtime of your programm.
    If you need another db you make a new db connection with an other name and use that connection in the constructor of your query. thats it



  • Hello,

    to make easy solution I just changed the databaseName when I need the other database and go back to the first databaseName when I don't need the second db :

    databaseManager::databaseManager()
    {
    QDir dir;
    dir.setPath("./Databases");
    if (!dir.exists()){
        dir.mkdir(".");
    }
    db=QSqlDatabase::addDatabase("QSQLITE");
    databaseName="./Databases/battery.sqlite";
    db.setDatabaseName(databaseName);
    dbInitCheckStep();
    }
    
    void databaseManager::dbInitCheckStep()
    {
       db.open();
       {
            QSqlQuery query;
            qDebug()<<"query.exec()"<< query.exec("CREATE TABLE IF NOT EXISTS check_data (step TEXT, periodical_CheckOk TEXT, periodical_CheckNo TEXT,regular_CheckOk TEXT, regular_CheckNo TEXT,general_OverhaulOk TEXT, general_OverhaulNo TEXT, rtos_aft_short_storageOk TEXT, rtos_aft_short_storageNo TEXT)");
            query.finish();
            query.clear();
            db.close();
        }
        QSqlDatabase::removeDatabase("QSQLITE");
    }
    
    QStringList databaseManager::comboListOtMat()
    {
        QStringList listCheck;
    
        db.setDatabaseName("../Software32/Databases/kardexdb.sqlite");
        db.open();
        {
            QSqlQuery query;
            query.exec("SELECT otnumber FROM ot_log where ottype='EQUIPMENT' and description='BATTERY' order by cast (substr(otnumber, -2) as decimal) desc, cast(substr(otnumber,1,2) as decimal) desc");
            while(query.next())
            {
                qDebug()<<query.value(0).toString();
                listCheck<<query.value(0).toString();
            }
            query.finish();
            query.clear();
            db.close();
        }
    
        QSqlDatabase::removeDatabase("QSQLITE");
        db.setDatabaseName("./Databases/battery.sqlite");
        return listCheck;
    }
    

    it's not beautyfull but it's easy and it's working fine ;-)


Log in to reply