Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A better function (inspired by the Qt source code : <a href="http://qt.gitorious.org/qt/qt/blobs/4.7/src/sql/kernel/qsqlresult.cpp#line644" rel="noreferrer">http://qt.gitorious.org/qt/qt/blobs/4.7/src/sql/kernel/qsqlresult.cpp#line644</a>).</p> <p>This function should handle almost all cases : This code does not work with Oracle DB when using Name Binding (This is the only DB that natively support Name Binding => executedQuery() do not return the query with '?' but the original query...)</p> <p>To be able to support native support Name Binding of DB, the keys of bound values must be sorted by length, then loop over the sorted map...</p> <pre><code>QString getLastExecutedQuery(const QSqlQuery&amp; query) { QString sql = query.executedQuery(); const int nbBindValues = query.boundValues().size(); for(int i = 0, j = 0; j &lt; nbBindValues; ++j) { i = sql.indexOf(QLatin1Char('?'), i); if (i &lt;= 0) { break; } const QVariant &amp;var = query.boundValue(j); QSqlField field(QLatin1String(""), var.type()); if (var.isNull()) { field.clear(); } else { field.setValue(var); } QString formatV = query.driver()-&gt;formatValue(field); sql.replace(i, 1, formatV); i += formatV.length(); } return sql; } </code></pre> <p>Edit: I found a bug in the previous function, if a '?' exists inside a quoted string, the '?' is replaced by the next available value. The bug already exists in Qt source code. This function should fix this problem (Could be improved a lot, but the idea is there)</p> <pre><code>QString getLastExecutedQuery(const QSqlQuery&amp; query) { QString sql = query.executedQuery(); int nbBindValues = query.boundValues().size(); for(int i = 0, j = 0; j &lt; nbBindValues;) { int s = sql.indexOf(QLatin1Char('\''), i); i = sql.indexOf(QLatin1Char('?'), i); if (i &lt; 1) { break; } if(s &lt; i &amp;&amp; s &gt; 0) { i = sql.indexOf(QLatin1Char('\''), s + 1) + 1; if(i &lt; 2) { break; } } else { const QVariant &amp;var = query.boundValue(j); QSqlField field(QLatin1String(""), var.type()); if (var.isNull()) { field.clear(); } else { field.setValue(var); } QString formatV = query.driver()-&gt;formatValue(field); sql.replace(i, 1, formatV); i += formatV.length(); ++j; } } return sql; } </code></pre>
 

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