Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>An alternative to what <a href="https://stackoverflow.com/users/711105/lightstep">lightstep</a> suggested, is to prepare query strings and then call a function that first writes the query to the log and only then calls real execute(). I personally use QString::arg() and "%number" for arguments to make a query string instead of bindValue().</p> <p>Let's sum things up:</p> <p>Solution #1 (<a href="https://stackoverflow.com/users/711105/lightstep">lightstep</a>)</p> <blockquote> <p>I came up with this workaround:</p> <pre><code>QString getLastExecutedQuery(const QSqlQuery&amp; query) { QString str = query.lastQuery(); QMapIterator&lt;QString, QVariant&gt; it(query.boundValues()); while (it.hasNext()) { it.next(); str.replace(it.key(),it.value().toString()); } return str; } </code></pre> </blockquote> <p>Solution #2 (me):</p> <pre><code>// my helper function #define SQLDB_SHOW_QUERIES #define SQLDB_LOG_QUERIES #define SQLDB_LOG_FILENAME "sqlite.db.log" bool executeQuery(QSqlQuery&amp; queryObject, const QString&amp; query) { bool result = true;; #ifdef SQLDB_SHOW_QUERIES std::cout&lt;&lt;query.toStdString()&lt;&lt;std::endl; #endif #ifdef SQLDB_LOG_QUERIES std::fstream fs_log; fs_log.open(SQLDB_LOG_FILENAME,std::ios::out|std::ios::app); if (fs_log.is_open()) { fs_log&lt;&lt;query.toUtf8().data()&lt;&lt;std::endl; } #endif result &amp;= queryObject.exec(query); #ifdef SQLDB_SHOW_QUERIES if (!result) std::cout&lt;&lt;queryObject.lastError().text().toStdString()&lt;&lt;std::endl; std::cout&lt;&lt;std::endl; #endif #ifdef SQLDB_LOG_QUERIES if (fs_log.is_open()) { if (!result) fs_log&lt;&lt;queryObject.lastError().text().toUtf8().data()&lt;&lt;std::endl; fs_log&lt;&lt;std::endl; fs_log.close(); } #endif return result; } // your sample code QSqlQuery myQuery(db); QString query = QString("INSERT INTO mytable VALUES (%1,%2)") .arg(1).arg(2); executeQuery(myQuery,query); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload