Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6660459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:09:29+00:00 2026-05-26T02:09:29+00:00

I got confused with the manual , should i work like this: { QSqlDatabase

  • 0

I got confused with the manual , should i work like this:

{
 QSqlDatabase db = QSqlDatabase::addDatabase (...);
 QSqlQuery query (db);
 query.exec (...);
}

QSqlDatabase::removeDatabase (...);

As the document points out, query or db will be deconstructed automatically.
But is that efficient ?

Well , if i cache db inside a class , like the following:

class Dummy {
  Dummy() { 
    db = QSqlDatabase::addDatabase (...);
  }
  ~Dummy() {
    db.close();
  }

  bool run() {
    QSqlQuery query (db);
    bool retval = query.exec (...);
    blabla ...
  }

  private:
    QSqlDatabase db;
};

Sometimes i could see warnings like:

QSqlDatabasePrivate::removeDatabase: connection 'BLABLA' is still in use, all queries will cease to work.

Even if i didn’t call run().

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:09:29+00:00Added an answer on May 26, 2026 at 2:09 am

    When you create a QSqlDatabase object with addDatabase or when you call removeDatabase, you are merely associating or disassociating a tuple (driver, hostname:port, database name, username/password) to a name (or to the default connection name if you don’t specify a connection name).
    The SQL driver is instantiated, but the database will only be opened when you call QSqlDatabase::open.

    That connection name is defined application-wide. So if you call addDatabase in each of the objects that use it, you are changing all QSqlDatabase objects that uses the same connection name and invalidating all queries that were active on them.

    The first code example you cited shows how to correctly disassociate the connection name, by ensuring that:

    • all QSqlQuery are detached from the QSqlDatabase before closing the database by calling QSqlQuery::finish(), which is automatic when the QSqlQuery object goes out of scope,
    • all QSqlDatabase with the same connection name are close()d when you call QSqlDatabase::removeDatabase (close() is also called automatically when the QSqlDatabase object goes out of scope).

    When you create the QSqlDatabase, depending on whether you want the connection to stay open for the application lifetime (1) or just when needed (2), you can:

    1. keep a single QSqlDatabase instance in one single class (for example, in your mainwindow), and use it in other objects that needs it either by passing the QSqlDatabase directly or just the connection name that you pass to QSqlDatabase::database to get the QSqlDatabase instance back. QSqlDatabase::database uses QHash to retrieve a QSqlDatabase from its name, so it is probably negligibly slower than passing the QSqlDatabase object directly between objects and functions, and if you you use the default connection, you don’t even have to pass anything anywhere, just call QSqlDatabase::database() without any parameter.

      // In an object that has the same lifetime as your application
      // (or as a global variable, since it has almost the same goal here)
      QSqlDatabase db;
      
      // In the constructor or initialization function of that object       
      db = QSqlDatabase::addDatabase("QSQLDRIVER", "connection-name"); 
      db.setHostname(...);
      // ...
      if(!this->db.open())  // open it and keep it opened
      {
          // Error handling...
      }
      
      // --------
      // Anywhere you need it, you can use the "global" db object 
      // or get the database connection from the connection name        
      QSqlDatabase db = QSqlDatabase::database("connection-name"); 
      QSqlQuery query(db);             
      
    2. configure the QSqlDatabase once, open it to test that the parameters are correct, and ditch the instance. The connection name, will still be accessible anywhere, but the database will have to be reopened:

      {
          // Allocated on the stack
          QSqlDatabase db = QSqlDatabase::addDatabase("QSQLDRIVER", "connection-name"); 
          db.setHostname(...);
          // ...
          if(!this->db.open()) // test the connection
          {
             // Error handling
          }
      // db is closed when it goes out of scope
      } 
      
      {
          // Same thing as for (1), but by default database() opens 
          // the connection if it isn't already opened 
          QSqlDatabase db = QSqlDatabase::database("connection-name"); 
          QSqlQuery query(db);
      
      // if there is no other connection open with that connection name,
      // the connection is closed when db goes out of scope
      } 
      

      In that case, note that you shouldn’t close the database explicitly, because you can have multiple objects using the same database connection in a reentrant manner (for example, if a function A use the connection and calls B which also use the connection. If B closes the connection before returning control to A, the connection will also be closed for A, which is probably a bad thing).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm relatively confused about this... I've got a table like: +----------------+--------------------------------------------------+------+-----+-------------------+-----------------------------+ | Field |
I was actually going through the question 1636644 .. and got confused with this
I have the following code and got myself confused: I have a query that
I was reading Google C++ Style Guide, and got confused in the Doing Work
I got confused with this two lines of coding : this.class.methods.name This is called
I've been following along with this excellent asio tutorial , but have got confused
I was reading this Freuqent Java concurrency problems question and got confused by an
This was asked to me in an interview! i really got confused How do
I got confused of some Rails' concepts like: gemset, rubygems, bundler . I have
i've got confused trying to change the map controls like said here But don`t

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.