Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Multiple DB SQLITE
QtWS25 Last Chance

Multiple DB SQLITE

Scheduled Pinned Locked Moved Solved General and Desktop
sqlite3removedatabase
4 Posts 3 Posters 552 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    philxxx609
    wrote on last edited by philxxx609
    #1

    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

    jsulmJ 1 Reply Last reply
    0
    • P philxxx609

      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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • G Offline
        G Offline
        Gerd
        wrote on last edited by
        #3

        @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

        1 Reply Last reply
        1
        • P Offline
          P Offline
          philxxx609
          wrote on last edited by
          #4

          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 ;-)

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved