<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QSQLite correct way to close and re-open]]></title><description><![CDATA[<p dir="auto">He, need to close QSQlite DB  .... update it ... and re open on app .... unfortunately I have some TableView open during these exchange ... so obtain these error ....</p>
<pre><code>QSqlDatabasePrivate::removeDatabase: connection 'Db2' is still in use, all queries will cease to work.
/* lastError qDebug messages lasterror */  QSqlError("", "Driver not loaded", "Driver not loaded")
</code></pre>
<p dir="auto">I close and re-open in these way (sorry for usleep, close your eyes on these) ....</p>
<pre><code>void MainWindow::reopenDB(){

    QSqlDatabase Db2= QSqlDatabase::database("db2");
    Db2.close();
    Db2.removeDatabase("db2");
    qDebug() &lt;&lt; "Db2 ... CLOSE DB... problem..." &lt;&lt; Db2.lastError();
    usleep(100000);
    Db2 = QSqlDatabase::addDatabase("QSQLITE", "db2");
    Db2.setDatabaseName("/home/mypath/DB2.sqlite3");
    qDebug() &lt;&lt; "Db2 ... REOPENdb... problem..." &lt;&lt; Db2.lastError();

    if (!Db2.open())
    {
        QMessageBox msgBoxD2B;
        msgBoxD2B.setStyleSheet("background-color:gray");
        msgBoxD2B.setIcon(QMessageBox::Critical);
        msgBoxD2B.setWindowTitle("ERROR D.B.");
        msgBoxD2B.setText("Impossicle open db");
        msgBoxD2B.setInformativeText("Please restarat app");
        msgBoxD2B.setStandardButtons(QMessageBox::Ok);
        msgBoxD2B.setDefaultButton(QMessageBox::Ok);
        int retD2B = msgBoxD2B.exec();
        switch (retD2B) {
          case QMessageBox::Ok:
              break;
          default:
              break;
        }
    }
    else{

        Db2 = QSqlDatabase::database("Db2");
        usleep(50000);

        sqlViewF();
        sqlViewE();
    }
}

</code></pre>
<p dir="auto">where are the problem that not see it??</p>
<p dir="auto">real thanks</p>
]]></description><link>https://forum.qt.io/topic/137850/qsqlite-correct-way-to-close-and-re-open</link><generator>RSS for Node</generator><lastBuildDate>Sat, 01 Aug 2026 23:08:20 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/137850.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 12 Jul 2022 19:15:51 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QSQLite correct way to close and re-open on Mon, 18 Jul 2022 08:04:37 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a> OK .... leave from void Minwindows::Mainwindows ..... open db command and unse only reopenDB call for that db and all work like a charm.</p>
<p dir="auto">thanks a lot.</p>
]]></description><link>https://forum.qt.io/post/721747</link><guid isPermaLink="true">https://forum.qt.io/post/721747</guid><dc:creator><![CDATA[gfxx]]></dc:creator><pubDate>Mon, 18 Jul 2022 08:04:37 GMT</pubDate></item><item><title><![CDATA[Reply to QSQLite correct way to close and re-open on Thu, 14 Jul 2022 17:44:52 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a> yes here:</p>
<pre><code>MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui-&gt;setupUi(this);

QSqlDatabase DB2= QSqlDatabase::addDatabase("QSQLITE", "Db2");
    DB2.setDatabaseName("/home/mypath/DB2.sqlite3");
    QSqlDatabase Dg = QSqlDatabase::addDatabase("QSQLITE", "g");
    Dg.setDatabaseName("/home/mypath/g.sqlite3");

    if (!Db2.open() || !Dg.open())
    { /*  etc etc .... */
</code></pre>
<p dir="auto">when start the app ..... so need to call only one time .... usign reopenDB() call from mainwindows too? good to know. Is these?</p>
<p dir="auto">thanks</p>
]]></description><link>https://forum.qt.io/post/721459</link><guid isPermaLink="true">https://forum.qt.io/post/721459</guid><dc:creator><![CDATA[gfxx]]></dc:creator><pubDate>Thu, 14 Jul 2022 17:44:52 GMT</pubDate></item><item><title><![CDATA[Reply to QSQLite correct way to close and re-open on Thu, 14 Jul 2022 12:43:56 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/gfxx">@<bdi>gfxx</bdi></a> said in <a href="/post/721428">QSQLite correct way to close and re-open</a>:</p>
<blockquote>
<p dir="auto">QSqlDatabasePrivate::addDatabase: duplicate connection name 'Db2', old connection removed.<br />
QSqlDatabasePrivate::addDatabase: duplicate connection name 'g', old connection removed.</p>
</blockquote>
<p dir="auto">Do you call addDatabase somewhere else (not only in reopenDB())?</p>
]]></description><link>https://forum.qt.io/post/721430</link><guid isPermaLink="true">https://forum.qt.io/post/721430</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Thu, 14 Jul 2022 12:43:56 GMT</pubDate></item><item><title><![CDATA[Reply to QSQLite correct way to close and re-open on Thu, 14 Jul 2022 12:38:39 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a>  thanks a lot ... after these:</p>
<p dir="auto">i try variuos scenario .... al least error become from that wrong code:</p>
<pre><code>void MainWindow::showUploadDb(int pageIdx){


    QSqlDatabase::database("LavDb").close();  /* &lt;- these the problem */

    closeDB();

/** my code */
}
</code></pre>
<p dir="auto">after change these into these one:</p>
<pre><code>void MainWindow::showUploadDb(int pageIdx){


    closeDB();

/** my code */
}
</code></pre>
<p dir="auto">plus closeDB:</p>
<pre><code>void MainWindow::closeDB(){
    stopChartF = true;  /* only a relais */ 
    QSqlDatabase::removeDatabase("Db2");
    QSqlDatabase::removeDatabase("g");  /* these is a therd db open for test pourpuse and hel me ... because no query on it at all */
    QTimer::singleShot(400, this, SLOT(reopenDB()));
}
</code></pre>
<p dir="auto">plus reopenDB:</p>
<pre><code>void MainWindow::reopenDB(){

    QSqlDatabase DB2= QSqlDatabase::addDatabase("QSQLITE", "Db2");
    DB2.setDatabaseName("/home/mypath/DB2.sqlite3");
    QSqlDatabase Dg = QSqlDatabase::addDatabase("QSQLITE", "g");
    Dg.setDatabaseName("/home/mypath/g.sqlite3");
    //qDebug() &lt;&lt; "DB2 ... REOPENdb... problemi ..." &lt;&lt; DB2.lastError();

    if (!Db2.open() || !Dg.open())
    {
        QMessageBox msgBoxD1B;
        msgBoxD1B.setStyleSheet("background-color:gray");
        msgBoxD1B.setIcon(QMessageBox::Critical);
        msgBoxD1B.setWindowTitle("ERROR D.B.");
        msgBoxD1B.setText("Impossibile connect to the world DB");
        msgBoxD1B.setInformativeText("restart");
        msgBoxD1B.setStandardButtons(QMessageBox::Ok);
        msgBoxD1B.setDefaultButton(QMessageBox::Ok);
        int retD1B = msgBoxD1B.exec();
        switch (retD1B) {
          case QMessageBox::Ok:

              break;
          default:
              // should never be reached
              break;
        }
    }
    else{

        Db2 = QSqlDatabase::database("Db2");
        Dg = QSqlDatabase::database("g");
        usleep(50000);
        stopChartF = false; /* only a relais */
        usleep(1000);

        sqlViewFupdate);
        sqlViewBupdate();
    }
}
</code></pre>
<p dir="auto">all work as aspected and no error .... but these warning not understand at all ..... (remember "g" have not query at all, no table model or other view type at all, only db open ...)</p>
<pre><code>QSqlDatabasePrivate::addDatabase: duplicate connection name 'Db2', old connection removed.
QSqlDatabasePrivate::addDatabase: duplicate connection name 'g', old connection removed.
</code></pre>
<p dir="auto">why these .... In my opinion I close all correct, and reopen in correct way ... or not?</p>
<p dir="auto">regards</p>
]]></description><link>https://forum.qt.io/post/721428</link><guid isPermaLink="true">https://forum.qt.io/post/721428</guid><dc:creator><![CDATA[gfxx]]></dc:creator><pubDate>Thu, 14 Jul 2022 12:38:39 GMT</pubDate></item><item><title><![CDATA[Reply to QSQLite correct way to close and re-open on Wed, 13 Jul 2022 09:24:55 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/gfxx">@<bdi>gfxx</bdi></a><br />
No it was not a grammar correction, just what you may have to do to truly close the SQLite file database, which is what you asked.</p>
<p dir="auto">If all you want to do is copy a file from client to server I do not see that you absolutely <em>have to</em> delete the <code>QSqlTableModel</code> prior to the operation.  It might be <em>advisable</em>, but it may be sufficient to ensure that you have not previously done any <em>updates</em> to the file in the client.</p>
<p dir="auto">Having said that, I think your code is a fair example of releasing the <code>QSqlDatabase</code> and then removing it anyway.  But you must not then keep any <code>QTableView</code> around whose model is the <code>QSqlTableModel</code>.</p>
]]></description><link>https://forum.qt.io/post/721216</link><guid isPermaLink="true">https://forum.qt.io/post/721216</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 13 Jul 2022 09:24:55 GMT</pubDate></item><item><title><![CDATA[Reply to QSQLite correct way to close and re-open on Wed, 13 Jul 2022 09:15:33 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a>  is english grammar correction? Thanks ;)) if not ..... seems is what <a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a>  think .... at these point think better stategy can be update db only without close it ....</p>
<p dir="auto">[premise:<br />
my intent is read a db2 and show some table in a server PC, after via TCP need to copy entire db2 from a machine CLIENT that is a producer of data, than read again db2 on server and show again some table. Unfortunately all machinery use SQlite db. ]</p>
<p dir="auto">maybe better stategy was copy from client to server db2(client side) as db2backup(server side) .... and update table from db2backup to db2 .... <strong>in these way need to delete QSqlTableModel during update operation?</strong></p>
]]></description><link>https://forum.qt.io/post/721214</link><guid isPermaLink="true">https://forum.qt.io/post/721214</guid><dc:creator><![CDATA[gfxx]]></dc:creator><pubDate>Wed, 13 Jul 2022 09:15:33 GMT</pubDate></item><item><title><![CDATA[Reply to QSQLite correct way to close and re-open on Wed, 13 Jul 2022 08:47:11 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/gfxx">@<bdi>gfxx</bdi></a><br />
You may have to <em>delete</em> the <code>QSqlTableModel</code> and then close and remove the database?</p>
]]></description><link>https://forum.qt.io/post/721205</link><guid isPermaLink="true">https://forum.qt.io/post/721205</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 13 Jul 2022 08:47:11 GMT</pubDate></item><item><title><![CDATA[Reply to QSQLite correct way to close and re-open on Wed, 13 Jul 2022 08:22:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> said in <a href="/post/721161">QSQLite correct way to close and re-open</a>:</p>
<blockquote>
<p dir="auto">You're not right - the QSqlTableModel holds the QSqlQuery and therefore a database connection.</p>
</blockquote>
<p dir="auto">so seems to need to stop QSqlTableModel  first .... than close db .... right?</p>
]]></description><link>https://forum.qt.io/post/721200</link><guid isPermaLink="true">https://forum.qt.io/post/721200</guid><dc:creator><![CDATA[gfxx]]></dc:creator><pubDate>Wed, 13 Jul 2022 08:22:40 GMT</pubDate></item><item><title><![CDATA[Reply to QSQLite correct way to close and re-open on Wed, 13 Jul 2022 06:30:53 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/gfxx">@<bdi>gfxx</bdi></a> said in <a href="/post/721160">QSQLite correct way to close and re-open</a>:</p>
<blockquote>
<p dir="auto">Because until now I think query for tableview can be consider a "oneshot" use of db ....when data is printed db work is end so can cloese it. Is not right?</p>
</blockquote>
<p dir="auto">You're not right - the QSqlTableModel holds the QSqlQuery and therefore a database connection.</p>
]]></description><link>https://forum.qt.io/post/721161</link><guid isPermaLink="true">https://forum.qt.io/post/721161</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Wed, 13 Jul 2022 06:30:53 GMT</pubDate></item><item><title><![CDATA[Reply to QSQLite correct way to close and re-open on Wed, 13 Jul 2022 06:22:34 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a>  .... ok but my question was these at end .... istance active can become from tableview? Or better show data with a select query on tableview is a persistent istance? so for first need to clear tableview and after can close db? Because until now I think query for tableview can be consider a "oneshot" use of db ....when data is printed db work is end so can cloese it. Is not right?</p>
<p dir="auto">Plus all my query is inside a specific void call everytime for a specific pourpose .... so think example:</p>
<pre><code>{
    QSqlDatabase db = QSqlDatabase::database("sales");
    QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
}
// Both "db" and "query" are destroyed because they are out of scope
QSqlDatabase::removeDatabase("sales"); // correct
</code></pre>
<p dir="auto">is equal to:</p>
<pre><code>void Mainwindows::sales()
{
    QSqlDatabase db = QSqlDatabase::database("sales");
    QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
}

void Mainwindows::closeDB(){
// Both "db" and "query" are destroyed because they are out of scope
QSqlDatabase::removeDatabase("sales"); // correct
 }
</code></pre>
<p dir="auto">thanks a lot</p>
]]></description><link>https://forum.qt.io/post/721160</link><guid isPermaLink="true">https://forum.qt.io/post/721160</guid><dc:creator><![CDATA[gfxx]]></dc:creator><pubDate>Wed, 13 Jul 2022 06:22:34 GMT</pubDate></item><item><title><![CDATA[Reply to QSQLite correct way to close and re-open on Tue, 12 Jul 2022 19:24:07 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">You have the explanation on how to do it in the <a href="https://doc.qt.io/qt-6/qsqldatabase.html#removeDatabase" target="_blank" rel="noopener noreferrer nofollow ugc">method documentation</a>.</p>
]]></description><link>https://forum.qt.io/post/721123</link><guid isPermaLink="true">https://forum.qt.io/post/721123</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Tue, 12 Jul 2022 19:24:07 GMT</pubDate></item><item><title><![CDATA[Reply to QSQLite correct way to close and re-open on Tue, 12 Jul 2022 19:22:16 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/gfxx">@<bdi>gfxx</bdi></a> said in <a href="/post/721120">QSQLite correct way to close and re-open</a>:</p>
<blockquote>
<p dir="auto">where are the problem that not see it??</p>
</blockquote>
<p dir="auto">You still have an instance of your db so it can't get closed.</p>
<pre><code>QSqlDatabase::database("db2").close()
QSqlDatabase::removeDatabase("db2");
</code></pre>
]]></description><link>https://forum.qt.io/post/721122</link><guid isPermaLink="true">https://forum.qt.io/post/721122</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Tue, 12 Jul 2022 19:22:16 GMT</pubDate></item></channel></rss>