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. When I want to open new connection for SQLite I faced with strange problem!!
Forum Updated to NodeBB v4.3 + New Features

When I want to open new connection for SQLite I faced with strange problem!!

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 5 Posters 759 Views 1 Watching
  • 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.
  • stackprogramerS Online
    stackprogramerS Online
    stackprogramer
    wrote on last edited by
    #1

    When I want to connect to a DB with the connection name according to the source code In debug output I am faced with some strange error..!!!
    And all query not works. Without a connection name, in the QSqlDatabase::addDatabase method, I had not any problem.
    How can open a new connection with a connection name?

            QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "connection");
          QString dbName = QDir::currentPath() + "/" + "scheduleddb";
          db.setDatabaseName(dbName);
          if (!db.open()) {
            qDebug() << "not connected to db: " << db.database() << " .";
          } else {
            qDebug() << " connected to db: " << db.database() << " .";
          }
    
    

    Eror:

    Connected to db:  QSqlDatabase(invalid)  .
    QSqlQuery::exec: database not open
    
    
    
    Christian EhrlicherC 1 Reply Last reply
    0
    • stackprogramerS stackprogramer

      When I want to connect to a DB with the connection name according to the source code In debug output I am faced with some strange error..!!!
      And all query not works. Without a connection name, in the QSqlDatabase::addDatabase method, I had not any problem.
      How can open a new connection with a connection name?

              QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "connection");
            QString dbName = QDir::currentPath() + "/" + "scheduleddb";
            db.setDatabaseName(dbName);
            if (!db.open()) {
              qDebug() << "not connected to db: " << db.database() << " .";
            } else {
              qDebug() << " connected to db: " << db.database() << " .";
            }
      
      

      Eror:

      Connected to db:  QSqlDatabase(invalid)  .
      QSqlQuery::exec: database not open
      
      
      
      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @stackprogramer said in When I want to open new connection for SQLite I faced with strange problem!!:

      QSqlQuery::exec: database not open

      This is not in your code above so please post the real code. Als QDir::currentPath() looks fishy - this directory can change e.g. during a call to QFileDialog (native mode) or others - don't use it for such stuff.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • M Offline
        M Offline
        m.hosseini
        wrote on last edited by
        #3

        QSqlDatabase _db;
        QString currentPath=QDir::currentPath();
        QString dbPath=currentPath+"/files/"+Database;//where Database is the name of you db for example : TestDB.db;

                QFile file(dbPath);
                if(file.exists())
                {
                    if (!QSqlDatabase::contains(Database))
                    {
                        _db = QSqlDatabase::addDatabase("QSQLITE",Database);
                        _db.setDatabaseName(dbPath);
                    }
                }
                else{
                    emit losingConnection_Signal();
                    return false;
                }
        

        _db.open();

        jsulmJ JonBJ stackprogramerS 3 Replies Last reply
        1
        • M m.hosseini

          QSqlDatabase _db;
          QString currentPath=QDir::currentPath();
          QString dbPath=currentPath+"/files/"+Database;//where Database is the name of you db for example : TestDB.db;

                  QFile file(dbPath);
                  if(file.exists())
                  {
                      if (!QSqlDatabase::contains(Database))
                      {
                          _db = QSqlDatabase::addDatabase("QSQLITE",Database);
                          _db.setDatabaseName(dbPath);
                      }
                  }
                  else{
                      emit losingConnection_Signal();
                      return false;
                  }
          

          _db.open();

          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • M m.hosseini

            QSqlDatabase _db;
            QString currentPath=QDir::currentPath();
            QString dbPath=currentPath+"/files/"+Database;//where Database is the name of you db for example : TestDB.db;

                    QFile file(dbPath);
                    if(file.exists())
                    {
                        if (!QSqlDatabase::contains(Database))
                        {
                            _db = QSqlDatabase::addDatabase("QSQLITE",Database);
                            _db.setDatabaseName(dbPath);
                        }
                    }
                    else{
                        emit losingConnection_Signal();
                        return false;
                    }
            

            _db.open();

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5
            This post is deleted!
            jsulmJ 1 Reply Last reply
            0
            • JonBJ JonB

              This post is deleted!

              jsulmJ Online
              jsulmJ Online
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • M m.hosseini

                QSqlDatabase _db;
                QString currentPath=QDir::currentPath();
                QString dbPath=currentPath+"/files/"+Database;//where Database is the name of you db for example : TestDB.db;

                        QFile file(dbPath);
                        if(file.exists())
                        {
                            if (!QSqlDatabase::contains(Database))
                            {
                                _db = QSqlDatabase::addDatabase("QSQLITE",Database);
                                _db.setDatabaseName(dbPath);
                            }
                        }
                        else{
                            emit losingConnection_Signal();
                            return false;
                        }
                

                _db.open();

                stackprogramerS Online
                stackprogramerS Online
                stackprogramer
                wrote on last edited by
                #7

                @m-hosseini Thanks very much. my problem is solved.

                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