<?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[QSql classes architecture]]></title><description><![CDATA[<p dir="auto">Hello.<br />
I am in the process of expirementing with Qt but i seem to have some issues regarding the software architecture of a database class.<br />
For my current program i have hundreds of queries, so i created a class for each table. These classes inherit from a base class called Database.</p>
<p dir="auto">Now, i'm not a very experience programmer, but i have around 30 tables to work with and i don't think is a good idea to have 30 classes just for the queries.<br />
What do you suggest i do ?<br />
(The solution that i need to find, must be avoiding the "QSqlDatabasePrivate::removeDatabase: connection 'FindItem' is still in use, all queries will cease to work." type of warnings).</p>
]]></description><link>https://forum.qt.io/topic/62464/qsql-classes-architecture</link><generator>RSS for Node</generator><lastBuildDate>Thu, 09 Apr 2026 10:26:15 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/62464.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 08 Jan 2016 14:44:19 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QSql classes architecture on Tue, 12 Jan 2016 10:28:37 GMT]]></title><description><![CDATA[<p dir="auto">You're welcome!</p>
]]></description><link>https://forum.qt.io/post/306870</link><guid isPermaLink="true">https://forum.qt.io/post/306870</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Tue, 12 Jan 2016 10:28:37 GMT</pubDate></item><item><title><![CDATA[Reply to QSql classes architecture on Tue, 12 Jan 2016 10:27:23 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a>  I see ! It is clear what you suggest.<br />
I think indeed this is a better way to proceed .<br />
Thank you for your time !</p>
]]></description><link>https://forum.qt.io/post/306869</link><guid isPermaLink="true">https://forum.qt.io/post/306869</guid><dc:creator><![CDATA[Koukoumaxe]]></dc:creator><pubDate>Tue, 12 Jan 2016 10:27:23 GMT</pubDate></item><item><title><![CDATA[Reply to QSql classes architecture on Tue, 12 Jan 2016 10:16:31 GMT]]></title><description><![CDATA[<p dir="auto">What I suggest is: decouple your interface from the database design. You can have a method for one query, you can have a method which executes several queries,... SQL database is an implementation detail and should not influence the interface too much.<br />
The user of the interface should not care about databases and how they are designed. They just use the interface to do what ever needs to be done. Ideally it should be possible to replace your SQL database with, for example, files or a non SQL database without changing the interface, or to change the database design without changing the interface (or with minor changes).<br />
Example: if you use QPushButton you don't care how a button is implemented on Windows, MacOS, Linux,... For you it is just a button.</p>
]]></description><link>https://forum.qt.io/post/306864</link><guid isPermaLink="true">https://forum.qt.io/post/306864</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Tue, 12 Jan 2016 10:16:31 GMT</pubDate></item><item><title><![CDATA[Reply to QSql classes architecture on Tue, 12 Jan 2016 09:53:53 GMT]]></title><description><![CDATA[<p dir="auto">I currently have a method to handle each query.<br />
So instead you suggest having something like this to be used for all the tables ?</p>
<pre><code>void addSomething(QString queryString){
         query.exec(queryString);
</code></pre>
<p dir="auto">And for SELECT queries i should use your solution with QDataWidgetMapper and QModel ?</p>
]]></description><link>https://forum.qt.io/post/306856</link><guid isPermaLink="true">https://forum.qt.io/post/306856</guid><dc:creator><![CDATA[Koukoumaxe]]></dc:creator><pubDate>Tue, 12 Jan 2016 09:53:53 GMT</pubDate></item><item><title><![CDATA[Reply to QSql classes architecture on Tue, 12 Jan 2016 09:26:45 GMT]]></title><description><![CDATA[<p dir="auto">I don't understand why you want to write something specific for each and every table?<br />
The methods I mentioned should have generic names like: void addSomething(const Something&amp;).<br />
"Something" could be stored in one or even more tables, the caller does simply not care about such details.<br />
So, the interface should not reflect your database design.</p>
<p dir="auto">It is not clear to me what you mean with security? The interface should not provide access to data which should be hidden and it does not matter how the interface is designed (whether you use one class per table or not). And who is "someone"? A developer using your interface or a real application user?</p>
]]></description><link>https://forum.qt.io/post/306843</link><guid isPermaLink="true">https://forum.qt.io/post/306843</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Tue, 12 Jan 2016 09:26:45 GMT</pubDate></item><item><title><![CDATA[Reply to QSql classes architecture on Tue, 12 Jan 2016 09:17:37 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a> I generaly use Inner join for queries affecting more than one table.<br />
When it comes to Inserting and Updating, i don't want the user to be able to add new data for an instrument for example, from a form about instrument methods.</p>
<p dir="auto">In the scenario you gave, i would still have to write a couple of methods for each of the tables and delete the unused method right ? I mean each table will still need methods for the queries.</p>
<p dir="auto">Also what about security ? I am not really sure, but isn't easier for someone to have access to an admin table data (when he shouldn't), when these data are stored in a class that he already has access in ?</p>
]]></description><link>https://forum.qt.io/post/306839</link><guid isPermaLink="true">https://forum.qt.io/post/306839</guid><dc:creator><![CDATA[Koukoumaxe]]></dc:creator><pubDate>Tue, 12 Jan 2016 09:17:37 GMT</pubDate></item><item><title><![CDATA[Reply to QSql classes architecture on Tue, 12 Jan 2016 08:55:51 GMT]]></title><description><![CDATA[<p dir="auto">How do you want to handle queries which affects more than one table with such approach?<br />
I would not use one class for each table, because then your design is affected by the number of tables in the database.<br />
What if you later add one table and remove two? You would need to write one new class and remove 2.<br />
I would use one class and implement methods to do specific tasks. This way in your code which uses this class you do not have to know how the data is stored (in which tables).</p>
]]></description><link>https://forum.qt.io/post/306836</link><guid isPermaLink="true">https://forum.qt.io/post/306836</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Tue, 12 Jan 2016 08:55:51 GMT</pubDate></item><item><title><![CDATA[Reply to QSql classes architecture on Tue, 12 Jan 2016 08:51:05 GMT]]></title><description><![CDATA[<p dir="auto">Do you think it will be a bad practice to use different class for each table ?<br />
Since each table has different columns and thus different queries.</p>
]]></description><link>https://forum.qt.io/post/306834</link><guid isPermaLink="true">https://forum.qt.io/post/306834</guid><dc:creator><![CDATA[Koukoumaxe]]></dc:creator><pubDate>Tue, 12 Jan 2016 08:51:05 GMT</pubDate></item><item><title><![CDATA[Reply to QSql classes architecture on Tue, 12 Jan 2016 08:45:00 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> Thank you very much.</p>
]]></description><link>https://forum.qt.io/post/306831</link><guid isPermaLink="true">https://forum.qt.io/post/306831</guid><dc:creator><![CDATA[Koukoumaxe]]></dc:creator><pubDate>Tue, 12 Jan 2016 08:45:00 GMT</pubDate></item><item><title><![CDATA[Reply to QSql classes architecture on Tue, 12 Jan 2016 08:06:55 GMT]]></title><description><![CDATA[<p dir="auto">Yes, the <a href="http://doc.qt.io/qt-5/qtsql-books-example.html" target="_blank" rel="noopener noreferrer nofollow ugc">Book Demonstration Example</a> shows you how to use it.</p>
]]></description><link>https://forum.qt.io/post/306824</link><guid isPermaLink="true">https://forum.qt.io/post/306824</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Tue, 12 Jan 2016 08:06:55 GMT</pubDate></item><item><title><![CDATA[Reply to QSql classes architecture on Tue, 12 Jan 2016 06:25:56 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> Wow ! that indeed will simplify my code ! From the first look, i can replace my set/get methods with these models right ?</p>
]]></description><link>https://forum.qt.io/post/306811</link><guid isPermaLink="true">https://forum.qt.io/post/306811</guid><dc:creator><![CDATA[Koukoumaxe]]></dc:creator><pubDate>Tue, 12 Jan 2016 06:25:56 GMT</pubDate></item><item><title><![CDATA[Reply to QSql classes architecture on Mon, 11 Jan 2016 22:02:34 GMT]]></title><description><![CDATA[<p dir="auto">You should take a look a <a href="http://doc.qt.io/qt-5/qdatawidgetmapper.html" target="_blank" rel="noopener noreferrer nofollow ugc">QDataWidgetMapper</a></p>
<p dir="auto">That will simplify your code.</p>
]]></description><link>https://forum.qt.io/post/306730</link><guid isPermaLink="true">https://forum.qt.io/post/306730</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Mon, 11 Jan 2016 22:02:34 GMT</pubDate></item><item><title><![CDATA[Reply to QSql classes architecture on Mon, 11 Jan 2016 08:48:38 GMT]]></title><description><![CDATA[<p dir="auto">Hello,<br />
Thank you for your response !<br />
<a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a>  No, everything is on the same database.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> So, for every form (QT window) that needs to have access to the data, i have 2 classes. One to setup the interface/tables/lineEdits e.t.c and the other class contains the database stuff (db info, queries ).</p>
<p dir="auto">This is an example of how my setup works for adding an entry to the table :</p>
<pre><code>void Instruments::addInstrument(){
    QSqlQuery query(QSqlDatabase::database(connectionName));

    query.prepare("INSERT INTO " + databaseName + ".lab_instruments(id, serial_no, manufacturer, model, description, "
                  "comm_port, comm_baud_rate, comm_data_bits, comm_parity, comm_stop_bits, installation_date, "
                  "last_service, next_service) "
                  "VALUES(:id, :serial_no, :manufacturer, :model, :description, :comm_port, :comm_baud_rate, :comm_data_bits, "
                  ":comm_parity, :comm_stop_bits, :installation_date, :last_service, :next_service )");

    query.bindValue(":id", regNo);
    query.bindValue(":serial_no", serialNo);
    query.bindValue(":manufacturer", manufacturer);
    query.bindValue(":model", model);
    query.bindValue(":description", description);
    query.bindValue(":comm_port", commPort);
    query.bindValue(":comm_baud_rate", baudRate);
    query.bindValue(":comm_data_bits", dataBits);
    query.bindValue(":comm_parity", parity);
    query.bindValue(":comm_stop_bits", stopBits);
    query.bindValue(":installation_date", installDate);
    query.bindValue(":last_service", lastService);
    query.bindValue(":next_service", nextService);

    if(!query.exec()) { qDebug()&lt;&lt;query.lastError(); }

}

</code></pre>
<p dir="auto">And this is the Qt method that handles the interface and calls this method :</p>
<pre><code>
void InstrumentsScreen::on_btnAdd_clicked()
{
    Instruments* instrument = new Instruments("addInstrument");

    //Extract data from text fields
    *instrument= updateValues(*instrument);
    //Update database
    instrument-&gt;connect();
    instrument-&gt;addInstrument();
    instrument-&gt;disconnect();
    QSqlDatabase::removeDatabase("addInstrument");
    initTable();
    on_btnClear_clicked();

}

Instruments InstrumentsScreen::updateValues(Instruments instrument){

    instrument.setRegNo(ui-&gt;txtRegNo-&gt;text());
    instrument.setSerialNo(ui-&gt;txtSerialNo-&gt;text());
    instrument.setManufacturer(ui-&gt;txtManufacturer-&gt;text());
    instrument.setModel(ui-&gt;txtModel-&gt;text());
    instrument.setDescription(ui-&gt;txtDescription-&gt;text());
    instrument.setCommPort(ui-&gt;txtCommPort-&gt;text());
    instrument.setBaudRate(ui-&gt;txtBaudRate-&gt;text());
    instrument.setDataBits(ui-&gt;txtDataBits-&gt;text());
    instrument.setParity(ui-&gt;cboxParity-&gt;currentText());
    instrument.setStopBits(ui-&gt;txtStopBits-&gt;text());
    instrument.setInstallationDate(ui-&gt;txtInstallDate-&gt;text());
    instrument.setLastService(ui-&gt;txtLastService-&gt;text());
    instrument.setNextService(ui-&gt;txtNextService-&gt;text());

}
</code></pre>
<p dir="auto">So this is just for a button on one of the forms.</p>
<p dir="auto">P.S: The reason for having each query in a method is because i feel like is bad programming having queries thrown all over the place in an interface class.</p>
]]></description><link>https://forum.qt.io/post/306621</link><guid isPermaLink="true">https://forum.qt.io/post/306621</guid><dc:creator><![CDATA[Koukoumaxe]]></dc:creator><pubDate>Mon, 11 Jan 2016 08:48:38 GMT</pubDate></item><item><title><![CDATA[Reply to QSql classes architecture on Fri, 08 Jan 2016 14:58:54 GMT]]></title><description><![CDATA[<p dir="auto">Hi and welcome to devnet,</p>
<p dir="auto">To add to <a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a>, are all your tables in a different database ?</p>
]]></description><link>https://forum.qt.io/post/306265</link><guid isPermaLink="true">https://forum.qt.io/post/306265</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Fri, 08 Jan 2016 14:58:54 GMT</pubDate></item><item><title><![CDATA[Reply to QSql classes architecture on Fri, 08 Jan 2016 14:50:36 GMT]]></title><description><![CDATA[<p dir="auto">Hi and welcome<br />
It sounds like a big setup. Could you perhaps tell a bit about what the<br />
queries do and maybe show 1 example of "class for each table"<br />
and what the program does to all this data?<br />
This way people here can much better suggest a design.</p>
]]></description><link>https://forum.qt.io/post/306263</link><guid isPermaLink="true">https://forum.qt.io/post/306263</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Fri, 08 Jan 2016 14:50:36 GMT</pubDate></item></channel></rss>