Solved update qsqlite and qtableview
-
@Mogli123 There is no WHERE in your query, so you're updating everything. If you, for example, want to update a data record with a specific ID you can do it like this:
UPDATE ToolChangeWarnings (COLOR, TOOLNR, TIME, POCKETSTATUS) " "VALUES (:COLOR, :TOOLNR, :TIME, :POCKETSTATUS)" "WHERE ID = :ID"
-
@jsulm
to updating everything it's ok, because the old data are not necessary after updating -
@Mogli123 But then I don't understand what the problem is.
Or maybe you misunderstood what I wrote? Without WHERE you're updating all data records in the table. So, if you have 10 records in that table all 10 will be updated. -
@jsulm
Yes that is that wat I want because I don't know which data had changed therefore I want to override all data.
Or is this a bad solution?
My problem is that it does not work and I don't know why.
after writing the data at first time it works fine and the qtableview is displaying them.
If I try to update them the qtableview does nothing
Furthermore I don't know if updating the database works because the qtableview shows no new data -
This post is deleted! -
if I try something like this
queryToolChangeWarnings->prepare("UPDATE ToolChangeWarnings SET TOOLNR = :TOOLNR"); queryToolChangeWarnings->bindValue(":TOOLNR", arrayToolChangeWarnings[0]);
The updating is working but complete column TOOLNR geht the int value from the arrayToolChangeWarnings[0]
How can I only update for example the first value from the column not the complete column
and so on -
Because your query asks exactly for that. Use the
WHERE
keyword and state precisely where you want the update to happen.See the documentation which also contain a big warning about missing the
WHERE
part.[edit: added link to documentation SGaist]
-
@SGaist
Sorry your right
If I try it lik this it worksqueryToolChangeWarnings->prepare("UPDATE ToolChangeWarnings SET TOOLNR = :TOOLNR WHERE ID = 0"); queryToolChangeWarnings->bindValue(":TOOLNR", arrayToolChangeWarnings[0]); queryToolChangeWarnings->exec();
It looks like that it is very impractical to do this for all cells
Do you know a solution to do this in a more efficient way -
Then use another bind value for
ID
and pass it also to your query. You can do that in a loop. -
@Mogli123
Provided I understand you right:It looks like that it is very impractical to do this for all cells
I believe you're saying you have some array of data (from a TCP socket). You want to update the database to change the
TOOLNR
value of a bunch of rows to a variety of values in your input data,It cannot be helped that this requires a distinct SQL statement to be sent to the database for each input value (array element). If you have a 100 rows of data you will be executing 100 SQL statements. That's how it is.
At the code side, as @SGaist says you do not have to write 100 lines of code, one for each row. You use a loop to produce the statements, like:
for (int i = 0; i < arrayToolChangeWarnings.size(); i++ { queryToolChangeWarnings->prepare("UPDATE ToolChangeWarnings SET TOOLNR = :TOOLNR WHERE ID = " + QString::number(i)); queryToolChangeWarnings->bindValue(":TOOLNR", arrayToolChangeWarnings[i]); queryToolChangeWarnings->exec(); }
There may be a way of sending the multiple statements as a single batch efficiently, I don't know (but then you'd have to deal with the bindings somehow).
-
-
Great !
Then please mark the thread as solved using the "Topic Tools" button so that other forum uses may know a solution has been found :)