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. Various Question about QSqlDatabase
QtWS25 Last Chance

Various Question about QSqlDatabase

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.7k 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.
  • M Offline
    M Offline
    Mar91
    wrote on last edited by
    #1

    Hi guys, i have various question about QSqlDatabase.
    My application contains 5 different classes, and in those 2 the same database is used.
    In each class there are 7-8 function, and in each function there a query is executed.

    1. to access the database from the class i have to declare QSqlDatabase db; it in the .h, is it good or not? Is there a better way to share it across the function in the same class?
    2. How can i completely close the connection and the database? I couldn't do it, in each try i made a message like: duplicate connection and so on was trown when trying to open another istance in the second class!
      This is my code right now:

    bla.h
    @
    Class bla {
    ...
    ..
    private:
    QSqlDatabase db;
    ....
    }
    @

    bla.cpp
    @
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("Application");
    if (!db.open()) {
    QMessageBox msgBox;
    msgBox.setText("Impossibile Aprire il DataBase!!");
    msgBox.exec();
    }
    @
    Destructor
    @
    bla::~bla() {
    ....
    db.close();
    }
    @

    There must be something wrong:(

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Your approach of using a member variable to hold your database is valid. For sharing the database across your classes, you can retreive the same database connection by giving it a name when you first create it (so do that only once!), and then retreiving the connection using QSqlDatabase::database().

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mar91
        wrote on last edited by
        #3

        ok, and to close the database, close the connection ?

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          Open the database in some main function (if not main() at all) and close it there too.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mar91
            wrote on last edited by
            #5

            the way to close it is:
            db.close();
            db.removedatabase(...)
            ?

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              Yes. But note that you should make sure that there are no queries using this database left in scope. See the documentation of QSqlDatabase::removeDatabase().

              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