0

I am inserting/updating a database table from a Qt application using QSqlTableModelmethods setData, submitAll and commit. For logging purposes, I need to record the corresponding SQL statements. How can I do that? (I am looking at QSqlTableModel::query() but seems as if it only returns SELECTs)

Here is a simplified version of what I am doing.

QSqlTableModel *_dataTableModel = qobject_cast<QSqlTableModel*>(dataTableView->model());
_dataTableModel->setData(item, value);
_dataTableModel->database().transaction();
_dataTableModel->submitAll();
_dataTableModel->database().commit()
QSqlQuery _currentQuery = _dataTableModel->query();
qDebug() << _currentQuery.lastQuery();

The last line always prints a SELECT, even when I do an update or an insert.


update

Here is a self-sufficient code snippet. The setup is that, I have a UI with a QTableView and a QPushButton. clicked() signal of push button is captured in addNewRow() slot and a arbitrary value is inserted to the first cell. I am printing the result of the lastQuery() expecting it to be the insert statement but it is a SELECT statement for the given table. What am I doing wrong?

main.cpp

{
    QApplication a(argc, argv);
    test1 w;
    // connect to and open database
    w.setDataTable("MY_TEST_TABLE");
    w.show();
    return a.exec();
}

test1.h

public:
    void setDataTable(QString dataTableName);

private slots:
    void addNewRow();

test1.cpp

void test1::setDataTable( QString dataTableName )
{
    QSqlTableModel *tableModel = new QSqlTableModel(); 
    tableModel->setTable(dataTableName);
    tableModel->select();   
    ui.tableView->setModel(tableModel);
}

void test1::addNewRow()
{
    QSqlTableModel *tableModel = qobject_cast<QSqlTableModel*>(ui.tableView->model());
    if(!tableModel->insertRow(tableModel->rowCount())) {
            return;
    }       
    tableModel->setData(tableModel->index(0, 0), QVariant("123345")); 
    tableModel->database().transaction(); 
    if(!tableModel->submitAll()) {
            return;
    }  
    if (!tableModel->database().commit()) {                                          
            return;
    }     
    qDebug() << tableModel->query().lastQuery();
} 
8
  • You would need to provide a self-contained example reproducing the issue. Please refer to sscce.org. Dec 26, 2013 at 7:59
  • This is still not "self-sufficient" example. It only has code chunks. Please provide an example that we can run off-hand, and introspect the operation. Please refer to sscce.org. Dec 26, 2013 at 11:42
  • I was not able to find a solution to this. As a reference to people who'd face the same problem in the future, you will have to explicitly create SQLs and use exec() and then executedQuery().
    – fortytwo
    Jan 3, 2014 at 5:41
  • that is also what I wrote, so what is left to be resolved here then? Jan 3, 2014 at 5:46
  • This is getting tired. You mentioned this method AND you also mentioned 'or you could also use the following method to query the last statement if it is done dynamically' and went on to describe how I can get last query from model. That second method is not working. Am I clear enough here?
    – fortytwo
    Jan 3, 2014 at 5:55

1 Answer 1

0

Based on the official documentation:

Inserting, Updating, and Deleting Records

QSqlQuery can execute arbitrary SQL statements, not just SELECTs. The following example inserts a record into a table using INSERT: QSqlQuery query; query.exec("INSERT INTO employee (id, name, salary) " "VALUES (1001, 'Thad Beaumont', 65000)");

This works with any statements, and not just select. If you do it this way in a raw mode, you could always build a QString first, and use that for the QSqlQuery::exec() operation and then log it, or you could also use the following method to query the last statement if it is done dynamically for instance:

QString QSqlQuery::lastQuery() const

Returns the text of the current query being used, or an empty string if there is no current query text.

So, the only remaining bit is how to get the query object from your model class. This can be done with the following method:

QSqlQuery QSqlQueryModel::query() const

Returns the QSqlQuery associated with this model.

22
  • Thank you. But the thing is that I do not construct a QSqlQuery. What I am doing is that using QSqlTableModel (to be precise, a subclass of it), setData() and then submit(). I implemented queryChange() to see if that query actually changes after submit and it does. Cannot figure out why I cant get the SQL with lastQuery(). Doing some fiddling, let's see where it gets.
    – fortytwo
    Dec 26, 2013 at 7:07
  • @RaggS: you can get the QSqlQSquery from QSqlTableModel by using the query() accessor method... Dec 26, 2013 at 7:12
  • Laszlo Papp> Added a code snippet to the original question for clarity.
    – fortytwo
    Dec 26, 2013 at 7:31
  • @Raggs: also, as I wrote in my answer, you can always construct the query yourself... Although, I find it a bit unusual to log the sql statements rather than Qt operations. Dec 26, 2013 at 7:49
  • Laszlo Papp> If you would read the question again, what I have stated is exactly what I was doing; QSqlTableModel setData, submitAll and commit and then querying.
    – fortytwo
    Dec 26, 2013 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.