<?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[Update record in tableView by QSqlQuery]]></title><description><![CDATA[<p dir="auto">Hi everyone,</p>
<p dir="auto">I try some options to change values in TableView (which is selectable) by using sql's UPDATE query.</p>
<p dir="auto">I built a code:</p>
<pre><code> query = QSqlQuery(db)
        iden = self.dlg.label_32.text()#Pobiera Identyfikator rekordu
        teryt = self.dlg.comboBox_10.currentText()#Pobiera teryt obiektu
        sql = "UPDATE ZARZADZANIE.ZAMOWIENIA SET ZAMAWIAJACY_KOD=:a,OBSZAR_KOD=:bb,...DATA_MOD_REK=:cca,TERYT=:ccb WHERE ID=:id"   
        sql = query.prepare(sql) 
        index =self.dlg.tableView_3.selectionModel().currentIndex()
        row = index.row()
        print (row)
        model = self.dlg.tableView_3.model()
        for col in range(model.columnCount()):
            rowData = [model.index(row, col).data() for col in range(model.columnCount()) ]
            literyBind = [':a',':bb',..':cca',':ccb',':id']    
            print (rowData[col])
            if rowData[col] is None:
                query.bindValue(literyBind[col],'')
            else:
                a=query.bindValue(literyBind[col],rowData[col])
</code></pre>
<p dir="auto">inspired by: <a href="https://www.qtcentre.org/threads/50864-qsqlquery-UPDATE-how-to" target="_blank" rel="noopener noreferrer nofollow ugc">https://www.qtcentre.org/threads/50864-qsqlquery-UPDATE-how-to</a><br />
(i'm not sure is it correct, but <code>query.exec_() </code>gains <code>TRUE</code>. I test it by changing 1 attribute in tableView)</p>
<p dir="auto">but I don't know how to put changes in Table View.</p>
<p dir="auto">I've tried by <code>self.model4.appendRow()</code>, or <code>self.model4.setItem(row, 0, QStandardItem(str(query.record().value(a[0]))))</code> or</p>
<p dir="auto"><code>QStandardItem(col.get(row))# col is self.model4.columnCount()</code><br />
but none of this work.</p>
<p dir="auto">Thanks for every help</p>
]]></description><link>https://forum.qt.io/topic/139474/update-record-in-tableview-by-qsqlquery</link><generator>RSS for Node</generator><lastBuildDate>Thu, 05 Nov 2026 02:07:23 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/139474.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 21 Sep 2022 08:53:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Update record in tableView by QSqlQuery on Wed, 21 Sep 2022 12:04:26 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/karoluss96">@<bdi>Karoluss96</bdi></a><br />
And?  Did you look at its documentation?</p>
<blockquote>
<p dir="auto">Returns the last query that was successfully executed.</p>
</blockquote>
<p dir="auto">You place it immediately <em>before</em> <code>query.exec_()</code>.  Why?</p>
<blockquote>
<p dir="auto">So maybe problem lays on query level</p>
</blockquote>
<p dir="auto">Sorry, but your whole code needs rewriting, more importantly you need to take time to <em>understand</em> what you are doing.  Same with some of your Python.  Sorry, but true.  Best of luck.</p>
]]></description><link>https://forum.qt.io/post/729627</link><guid isPermaLink="true">https://forum.qt.io/post/729627</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 21 Sep 2022 12:04:26 GMT</pubDate></item><item><title><![CDATA[Reply to Update record in tableView by QSqlQuery on Wed, 21 Sep 2022 12:03:04 GMT]]></title><description><![CDATA[<p dir="auto">The print in query.executedQuery():</p>
<pre><code>UPDATE ZARZADZANIE.ZAMOWIENIA SET ZAMAWIAJACY_KOD=:a,OBSZAR_KOD=:bb,....TERYT=:ccb WHERE ID=:id
</code></pre>
<p dir="auto">So maybe problem lays on query level</p>
]]></description><link>https://forum.qt.io/post/729625</link><guid isPermaLink="true">https://forum.qt.io/post/729625</guid><dc:creator><![CDATA[Karoluss96]]></dc:creator><pubDate>Wed, 21 Sep 2022 12:03:04 GMT</pubDate></item><item><title><![CDATA[Reply to Update record in tableView by QSqlQuery on Wed, 21 Sep 2022 11:59:56 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/karoluss96">@<bdi>Karoluss96</bdi></a> said in <a href="/post/729622">Update record in tableView by QSqlQuery</a>:</p>
<blockquote>
<pre><code>            rec= ''
            tm = ''    
            #query.bindValue(literyBind[col],'')
            sv= rec.setValue(literyBind[col],'')
</code></pre>
</blockquote>
<p dir="auto">If you just <em>look</em> at this how can it make any sense to set <code>rec</code> to an empty string and then call <code> sv= rec.setValue()</code>??</p>
<blockquote>
<pre><code>            sv=rec.setValue(literyBind[col],rowData[i])
            Uprow=tm.updateRowInTable(row,sv)
</code></pre>
</blockquote>
<p dir="auto">What does <code>rec.setValue()</code> return?  Nothing or a boolean?  Is that really what you want to try putting into your row?</p>
]]></description><link>https://forum.qt.io/post/729623</link><guid isPermaLink="true">https://forum.qt.io/post/729623</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 21 Sep 2022 11:59:56 GMT</pubDate></item><item><title><![CDATA[Reply to Update record in tableView by QSqlQuery on Wed, 21 Sep 2022 11:54:50 GMT]]></title><description><![CDATA[<p dir="auto">Ah wait I forgot show you this:</p>
<pre><code> tm =QSqlTableModel()
 rec= QSqlRecord()
mod = QSqlQueryModel()
</code></pre>
<p dir="auto">And after that I cleaned the code to:</p>
<pre><code> if rec is None or tm is None or rowData[col] is None:#Na wypadek jakby się pojawił
                rec= ''
                tm = ''    
                #query.bindValue(literyBind[col],'')
                sv= rec.setValue(literyBind[col],'')
            # else:
            #   pass  
                #query.bindValue(literyBind[col],rowData[col])
        print(query.executedQuery())
        if query.exec_():#query.next():
              for i in range(len(rowData)):
                sv=rec.setValue(literyBind[col],rowData[i])
                Uprow=tm.updateRowInTable(row,sv)
                print (Uprow)
</code></pre>
<p dir="auto">Yes, I also need a break and do sth diffrent in code</p>
]]></description><link>https://forum.qt.io/post/729622</link><guid isPermaLink="true">https://forum.qt.io/post/729622</guid><dc:creator><![CDATA[Karoluss96]]></dc:creator><pubDate>Wed, 21 Sep 2022 11:54:50 GMT</pubDate></item><item><title><![CDATA[Reply to Update record in tableView by QSqlQuery on Wed, 21 Sep 2022 11:56:24 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/karoluss96">@<bdi>Karoluss96</bdi></a><br />
I have no idea what your question is.  I do not understand your code or your approach.  You have a <code>rec= QSqlRecord()</code>.  What is <code>rec.setNull('')</code> supposed to do for you?  What about <code>rec=rec.value('')</code> which changes what <code>rec</code> is?  What does <code>print(query.executedQuery())</code> tell you if you have not executed <code>query</code> yet?  Where you go <code>rec= ''</code> you follow that with <code>sv= rec.setValue()</code>, how is that supposed to work?  Why are you using <code>QTableModel::updateRowInTable()</code> when the docs usually advise you would not call this?  Why are you calling it with <code>rowData[i]</code> (for <code>QSqlRecord values</code>) which seems to be one column in a row?</p>
<p dir="auto">You should be stepping through your code in a debugger to discover for yourself how it is behaving, not just throwing code at us and asking what is wrong.</p>
<p dir="auto">I will leave others to respond now, my <em>polite</em> advice is that you need to understand what you are doing and what you need to do before trying all this code you have.</p>
]]></description><link>https://forum.qt.io/post/729621</link><guid isPermaLink="true">https://forum.qt.io/post/729621</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 21 Sep 2022 11:56:24 GMT</pubDate></item><item><title><![CDATA[Reply to Update record in tableView by QSqlQuery on Wed, 21 Sep 2022 11:44:24 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a> said in <a href="/post/729619">Update record in tableView by QSqlQuery</a>:</p>
<blockquote>
<p dir="auto">range(len(rowData))</p>
</blockquote>
<p dir="auto">this function looks better but still have an error:<br />
updateRowInTable(self, int, QSqlRecord): argument 2 has unexpected type 'str'</p>
<p dir="auto">Meanwhile I'd found the setValue function in QSqlRecord, so now it looks that:</p>
<pre><code> if rowData[col] is None:
                query.bindValue(literyBind[col],'')
                rec.setNull('')
                rec=rec.value('')
            else:
                query.bindValue(literyBind[col],rowData[col])
        print(query.executedQuery())
        if query.exec_():#query.next():
            if rec is None or tm is None or rowData[col] is None:
                rec= ''
                tm = ''
                sv= rec.setValue(literyBind[col],'') 
            else:
              for i in range(len(rowData)):
                Uprow=tm.updateRowInTable(row,rowData[i])
                #sv.rec.setValue(literyBind[col],rowData[i])
                print (Uprow)
        else:
            print (query.lastError().text())
</code></pre>
]]></description><link>https://forum.qt.io/post/729620</link><guid isPermaLink="true">https://forum.qt.io/post/729620</guid><dc:creator><![CDATA[Karoluss96]]></dc:creator><pubDate>Wed, 21 Sep 2022 11:44:24 GMT</pubDate></item><item><title><![CDATA[Reply to Update record in tableView by QSqlQuery on Wed, 21 Sep 2022 11:31:41 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/karoluss96">@<bdi>Karoluss96</bdi></a> said in <a href="/post/729615">Update record in tableView by QSqlQuery</a>:</p>
<blockquote>
<p dir="auto">list indices must be integers or slices, not str, but this table (a list!) cannot be converted into int,</p>
</blockquote>
<p dir="auto">What about</p>
<pre><code>for i in range(len(rowData)):
</code></pre>
<p dir="auto">?<br />
Is a simple Python thing...</p>
]]></description><link>https://forum.qt.io/post/729619</link><guid isPermaLink="true">https://forum.qt.io/post/729619</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Wed, 21 Sep 2022 11:31:41 GMT</pubDate></item><item><title><![CDATA[Reply to Update record in tableView by QSqlQuery on Wed, 21 Sep 2022 11:14:51 GMT]]></title><description><![CDATA[<p dir="auto">Now it looks that:</p>
<pre><code>tm =QSqlTableModel()
        rec= QSqlRecord()
        mod = QSqlQueryModel()
        for col in range(model.columnCount()):
            rowData = [model.index(row, col).data() for col in range(model.columnCount()) ]
            print (rowData[0])
            literyBind = [':a',':bb',':bc'...':cca',':ccb',':id']
            if rowData[col] is None:
                query.bindValue(literyBind[col],'')
                rec.setNull('')
                rec=rec.value('')
            else:
                query.bindValue(literyBind[col],rowData[col])
        print(query.executedQuery())
        if query.exec_():#query.next():
            if rec is None or tm is None:
                rec= ''
                tm = ''
            for i in rowData:
                Uprow=tm.updateRowInTable(row,rowData[i])
                print (Uprow)
</code></pre>
<p dir="auto">in rowData[i] shout:  list indices must be integers or slices, not str, but this table (a list!) cannot be converted into int, as there's only one integer value in this table</p>
]]></description><link>https://forum.qt.io/post/729615</link><guid isPermaLink="true">https://forum.qt.io/post/729615</guid><dc:creator><![CDATA[Karoluss96]]></dc:creator><pubDate>Wed, 21 Sep 2022 11:14:51 GMT</pubDate></item><item><title><![CDATA[Reply to Update record in tableView by QSqlQuery on Wed, 21 Sep 2022 10:49:12 GMT]]></title><description><![CDATA[<p dir="auto">OK, I find a way:<br />
QSqlTableModel::updateRowInTable(int row, const QSqlRecord &amp;values) -&gt; row I have,  QSqlRecord::value(int index) but about the 2nd I'm not sure is good enough</p>
]]></description><link>https://forum.qt.io/post/729610</link><guid isPermaLink="true">https://forum.qt.io/post/729610</guid><dc:creator><![CDATA[Karoluss96]]></dc:creator><pubDate>Wed, 21 Sep 2022 10:49:12 GMT</pubDate></item><item><title><![CDATA[Reply to Update record in tableView by QSqlQuery on Wed, 21 Sep 2022 10:14:54 GMT]]></title><description><![CDATA[<p dir="auto">Well, Update is difficult as it change values after select another record.<br />
So that in my point of view (check If I'm thinking good!) binding values is compulsory, wheras QSqlRecord and QueryModel make values to be stable!?</p>
]]></description><link>https://forum.qt.io/post/729603</link><guid isPermaLink="true">https://forum.qt.io/post/729603</guid><dc:creator><![CDATA[Karoluss96]]></dc:creator><pubDate>Wed, 21 Sep 2022 10:14:54 GMT</pubDate></item><item><title><![CDATA[Reply to Update record in tableView by QSqlQuery on Wed, 21 Sep 2022 10:10:42 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/karoluss96">@<bdi>Karoluss96</bdi></a> said in <a href="/post/729596">Update record in tableView by QSqlQuery</a>:</p>
<blockquote>
<p dir="auto">OK: If you suggest the road through QSqlRecord: show/find me an example</p>
</blockquote>
<p dir="auto">Try Googling yourself, instead of asking me to show/find for you, I have other things to do.  I already gave you a link to an example in the Qt docs.  I am not here to show you examples all the time, it's you who is coding.</p>
]]></description><link>https://forum.qt.io/post/729601</link><guid isPermaLink="true">https://forum.qt.io/post/729601</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 21 Sep 2022 10:10:42 GMT</pubDate></item><item><title><![CDATA[Reply to Update record in tableView by QSqlQuery on Wed, 21 Sep 2022 09:47:06 GMT]]></title><description><![CDATA[<p dir="auto">OK: If you suggest the road through QSqlRecord: show/find me an example<br />
The road through QStandardItemModel was my first Idea, but I don't how to finish it!</p>
]]></description><link>https://forum.qt.io/post/729596</link><guid isPermaLink="true">https://forum.qt.io/post/729596</guid><dc:creator><![CDATA[Karoluss96]]></dc:creator><pubDate>Wed, 21 Sep 2022 09:47:06 GMT</pubDate></item><item><title><![CDATA[Reply to Update record in tableView by QSqlQuery on Wed, 21 Sep 2022 09:45:32 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/karoluss96">@<bdi>Karoluss96</bdi></a><br />
If you use <code>QTableModel</code> bound to <code>QTableView</code>, and you are happy there will be one table column per each column in the SQL table, the rows will be editable directly and you won't need to write the <code>UPDATE</code> statement or bind any values.  It will also allow for inserting or deleting rows.</p>
<p dir="auto">I am not going to guess whether this is or is not what you want.  If you want to stick with your way, and you want to re-read a SQL row after you have <code>UPDATE</code>d it and copy that into your <code>QStandardItemModel</code>, or you want to get the new values directly out of the values you bind to the <code>UPDATE</code> query and put them into the <code>QStandardItemModel</code> without re-querying the SQL database, go ahead and do so.</p>
]]></description><link>https://forum.qt.io/post/729594</link><guid isPermaLink="true">https://forum.qt.io/post/729594</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 21 Sep 2022 09:45:32 GMT</pubDate></item><item><title><![CDATA[Reply to Update record in tableView by QSqlQuery on Wed, 21 Sep 2022 09:33:41 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/729589">Update record in tableView by QSqlQuery</a>:</p>
<blockquote>
<p dir="auto">decide whether you have any reason to interpose a QStandardItemModel on top of your SQ</p>
</blockquote>
<p dir="auto">So query.bind value won't be useful?</p>
]]></description><link>https://forum.qt.io/post/729590</link><guid isPermaLink="true">https://forum.qt.io/post/729590</guid><dc:creator><![CDATA[Karoluss96]]></dc:creator><pubDate>Wed, 21 Sep 2022 09:33:41 GMT</pubDate></item><item><title><![CDATA[Reply to Update record in tableView by QSqlQuery on Wed, 21 Sep 2022 09:30:43 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/karoluss96">@<bdi>Karoluss96</bdi></a><br />
No, you need to start by reading how to to use <a href="https://doc.qt.io/qt-6/qsqltablemodel.html#details" target="_blank" rel="noopener noreferrer nofollow ugc">QTableModel</a>, verify it will let you do what you want to do, rather than randomly replacing a <code>QSqlQuery</code> with a <code>QSqlTableModel</code>.  Above all else you need to decide whether you have any reason to interpose a <code>QStandardItemModel</code> on top of your SQL.</p>
]]></description><link>https://forum.qt.io/post/729589</link><guid isPermaLink="true">https://forum.qt.io/post/729589</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 21 Sep 2022 09:30:43 GMT</pubDate></item><item><title><![CDATA[Reply to Update record in tableView by QSqlQuery on Wed, 21 Sep 2022 09:28:29 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/729583">Update record in tableView by QSqlQuery</a>:</p>
<blockquote>
<p dir="auto">You are making it really complicated by interposing any kind of QStandardItemModel.  Abandon your way of doing it and change over to using QSqlTableModel directly.  Then not only will there be no copying but also it will do the table UPDATE for you and read the updated row back into the model, which the QTableView will see automatically.</p>
</blockquote>
<p dir="auto">so at first I need to change?:</p>
<pre><code>query=QSqlTableModel(db)
</code></pre>
<p dir="auto">I change only one row- this which is current select.</p>
]]></description><link>https://forum.qt.io/post/729586</link><guid isPermaLink="true">https://forum.qt.io/post/729586</guid><dc:creator><![CDATA[Karoluss96]]></dc:creator><pubDate>Wed, 21 Sep 2022 09:28:29 GMT</pubDate></item><item><title><![CDATA[Reply to Update record in tableView by QSqlQuery on Wed, 21 Sep 2022 09:19:21 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/karoluss96">@<bdi>Karoluss96</bdi></a><br />
If you use <code>QSqlQuery</code> to do the <code>UPDATE</code>, you will have to re-issue your original query to re-<code>SELECT</code> the (all the) row(s) you want after the database change.  And then because you are using a <code>QStandardItemModel</code> you will have to re-populate it from the SQL result set.</p>
<p dir="auto">You are making it <em>really</em> complicated by interposing any kind of <code>QStandardItemModel</code>.  Abandon your way of doing it and change over to using <code>QSqlTableModel</code> directly.  Then not only will there be no copying but also it will do the table <code>UPDATE</code> for you <em>and</em> read the updated row back into the model, which the <code>QTableView</code> will see automatically.</p>
]]></description><link>https://forum.qt.io/post/729583</link><guid isPermaLink="true">https://forum.qt.io/post/729583</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 21 Sep 2022 09:19:21 GMT</pubDate></item><item><title><![CDATA[Reply to Update record in tableView by QSqlQuery on Wed, 21 Sep 2022 09:06:37 GMT]]></title><description><![CDATA[<p dir="auto">current end of code is:</p>
<pre><code> if query.exec_():#query.next():
            Uprow=QStandardItem(col.row) 
            self.model4.appendRow(Uprow) 
            print (Uprow)
        else:
            print (query.lastError().text())
</code></pre>
]]></description><link>https://forum.qt.io/post/729577</link><guid isPermaLink="true">https://forum.qt.io/post/729577</guid><dc:creator><![CDATA[Karoluss96]]></dc:creator><pubDate>Wed, 21 Sep 2022 09:06:37 GMT</pubDate></item></channel></rss>