Note that there are some explanatory texts on larger screens.

plurals
  1. POQt4 and MySQL5 - Binding QByteArray "1:1" as binary data
    text
    copied!<p>I have some issues with character encoding of a binary value using Qt4 and MySQL5.</p> <p>Let's say we want to bind a value containing the four bytes <code>\xDE \xAD \xBE \xEF</code>. I check the bound value using the MySQL function <code>HEX()</code> using this code:</p> <pre><code>#include &lt;QtGui/QApplication&gt; #include &lt;QDebug&gt; #include &lt;QSqlQuery&gt; #include &lt;QVariant&gt; #include &lt;QSqlRecord&gt; int main(int argc, char *argv[]) { QApplication a(argc, argv); QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); if(!db.open("test", "test")) exit(1); QSqlQuery q("SELECT HEX(?)"); q.addBindValue(QVariant(QByteArray::fromHex("DEADBEEF"))); if(!q.exec()) exit(1); if(!q.next()) exit(1); qDebug() &lt;&lt; q.record().value(0).toString(); return a.exec(); } </code></pre> <p>The output of this code is <code>"DEADEFBFBDEFBFBD"</code> which is obviously the HEX code of <code>\xDE \xAD \xBE \xEF</code> interpreted as a latin1-encoded string and then encoded as a UTF8 string.</p> <p>If I do not bind the value using addBindValue() but placing it directly into the query using <code>UNHEX('DEADBEEF')</code> results in the expected behaviour (which isn't surprising...).</p> <p>Where does the UTF8 encoding step take place?</p> <p>(Finally, I want to store a binary value "1:1" into a BLOB field.)</p> <p><strong>OS:</strong> Ubuntu 10.10 (32 bit)<br> <strong>Qt Version:</strong> 4.7.0 (Ubuntu package)<br> <strong>MySQL Version:</strong> 5.1.49-1ubuntu8.1</p> <p>Thanks in advance!</p>
 

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