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

  • Home
  • SEARCH
  • 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 9232773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:23:36+00:00 2026-06-18T06:23:36+00:00

I have widget which connects to database: Widget::Widget(QWidget *parent) { QString databaseName = name;

  • 0

I have widget which connects to database:

Widget::Widget(QWidget *parent)
{
    QString databaseName = "name";
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(databaseName);
    db.setHostName("localhost");
    if(!db.open())
        qDebug()<<"ret error";
}

Now I want to delete database connection after widget close (currently I get warnings like: QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection is still in use...). I’ ve read some topics and tried to evaluate some solution from them but none works for me. My code:

void Widget::closeEvent(QCloseEvent *e)
{
    QSqlDatabase db = QSqlDatabase::database();
    QString connection = db.connectionName();
    db.close();
    QSqlDatabase::removeDatabase(connection);
    qDebug()<<"error: "<<db.lastError().text();
}

Error I get is: Driver not loaded Driver not loaded

What is the correct way to do this?

Edit:

another method:

void Widget::someMethod()
{
    QSqlDatabase db = QSqlDatabase::database();
    QSqlQuery query(db);
    query.exec("some query");
}
  • 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-06-18T06:23:38+00:00Added an answer on June 18, 2026 at 6:23 am

    Try giving a connection name parameter(2nd parameter) in addDatabase() , that should solve your problem.

    Here is the modified code you could try:

    Widget::Widget(QWidget *parent)
    {
        QString databaseName = "name";
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "test_db_connection" );
        db.setDatabaseName(databaseName);
        db.setHostName("localhost");
        if(!db.open())
            qDebug()<<"ret error";
    }
    

    Here is a complete working code from my machine for sqlite database which you can use as reference:

    local_db = QSqlDatabase::addDatabase("QSQLITE","localdb");
    local_db.setDatabaseName("localdb.sqlite");
    local_db_query = QSqlQuery(local_db);
    local_db_query.prepare( "SELECT * FROM sample_table" );
    local_db_query.exec();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JavaScript widget which communicates with my Rails app by creating tags
In a C# desktop application, I have a customer widget which contains a text
I have a JS script (widget) which is added to other websites. Technically its
I did mockups in balsamiq & they have this nice widget, which Allowed to
I have a activity which has a button and 2 autocomplete widget. for the
I have the following variable: $argument = 'blue widget'; Which I pass in the
Suppose I have a MyWidget which contains a MySubWidget , e.g. a custom widget
I have a widget which changes when an option is toggled. This invalidates all
I have a widget which is injecting a popup into different web sites, as
I have a widget which can contain one or more QTableView child widgets. By

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.