Update QSqlTableModel after QSqlQuery executing
-
@SGaist said in Update QSqlTableModel after QSqlQuery executing:
QSqlDatabase manages the connections. When needed ask it for the connection you want to use.
Ah ok thanks.@JonB said in Update QSqlTableModel after QSqlQuery executing:
I suspect/wonder that the model does not get updated after column add/remove, rather than the view? I don't know what you think will cause the model to see the changed columns. It should be easy for you discover whether the issue lies at model side or view. You could check the query from QSqlTableModel::query(), or you could sub-class QSqlTableModel (IMHO always advisable) so that you can check QSqlTableModel::selectStatement().
Ok I don't see there my new column.
void redo() override { QString query_ = QString("ALTER TABLE %1 ADD COLUMN %2 %3").arg(m_table).arg(m_name).arg(m_type); QSqlQuery query("", *m_db); bool status = query.exec(query_); if (!status) { QString err = query.lastError().text(); //emit errorOccured(err); } query.finish(); m_model->selectStatement(); // I don't see the new column here m_model->setQuery(query); m_model->selectStatement(); // The selectStatement is empty m_model->setTable(m_model->tableName()); m_model->selectStatement(); // query is fine and the new column is shown in the table view }
So the question is, why in the second selectStatement() call the query is empty?
The idea is now to update the query of the model every time I'm executing a new query?@Wuzi said in Update QSqlTableModel after QSqlQuery executing:
m_model->selectStatement(); // I don't see the new column here m_model->setQuery(query); m_model->selectStatement(); // The selectStatement is empty //m_model->setTable(m_model->tableName()); m_model->selectStatement(); // query is fine and the new column is shown in the table view
I simply do not believe the "query is fine" in the third
selectStatement()
you show here. I can believe it would be fine if you uncommented thesetTable()
statement, but that is not what you show. Nonetheless is that what you mean??The idea is now to update the query of the model every time I'm executing a new query?
Not for every query, but yes for the very unusual case where you add/remove/alter columns. You have to tell
QSqlTableModel
to re-read the table definition, andsetTable()
should do that.Finally, I cannot imagine why you persist in
m_model->setQuery(query);
. Your query is anALTER TABLE
statement. that is totally unsuitable as the statement for reading data from a table, which should be aSELECT
statement. You don't need to set that (setTable()
will sort it out), but don't set it to some completely unrelated statement. -
@Wuzi said in Update QSqlTableModel after QSqlQuery executing:
m_model->selectStatement(); // I don't see the new column here m_model->setQuery(query); m_model->selectStatement(); // The selectStatement is empty //m_model->setTable(m_model->tableName()); m_model->selectStatement(); // query is fine and the new column is shown in the table view
I simply do not believe the "query is fine" in the third
selectStatement()
you show here. I can believe it would be fine if you uncommented thesetTable()
statement, but that is not what you show. Nonetheless is that what you mean??The idea is now to update the query of the model every time I'm executing a new query?
Not for every query, but yes for the very unusual case where you add/remove/alter columns. You have to tell
QSqlTableModel
to re-read the table definition, andsetTable()
should do that.Finally, I cannot imagine why you persist in
m_model->setQuery(query);
. Your query is anALTER TABLE
statement. that is totally unsuitable as the statement for reading data from a table, which should be aSELECT
statement. You don't need to set that (setTable()
will sort it out), but don't set it to some completely unrelated statement.@JonB said in Update QSqlTableModel after QSqlQuery executing:
I simply do not believe the "query is fine" in the third selectStatement() you show here. I can believe it would be fine if you uncommented the setTable() statement, but that is not what you show.
@JonB said in Update QSqlTableModel after QSqlQuery executing:
Finally, I cannot imagine why you persist in m_model->setQuery(query);. Your query is an ALTER TABLE statement. that is totally unsuitable
You are right. SELECT is the correct one
Sorry. I played around to check and forgott to enable it again. I edited the code above
Yes I think setTable and does the job. I thought there is a different way doing it.
Thank you all for helping!