[Qt-interest] QtSql - fail to send blob data

Robert Hairgrove evorgriahr at hispeed.ch
Thu Sep 8 16:45:49 CEST 2011


On Thu, 2011-09-08 at 16:29 +0200, Cyril wrote:
> Thanks for your answers.
> I am using MySQL server 5.5.8, MySQL database driver of Qt 4.7 ,
> libmysql 6.0.2, under windows 7.
> I tried the CONCAT syntax using hex-encoded blob but it still fails
> the same way. Here is the code :
> 
> 
> {
>   QSqlQuery q1(db);
>   q1.prepare("INSERT INTO t (id, data) VALUES(42, '')"); // field data
> is a LONGBLOB
>   q1.exec();
> }
> 
> 
> QByteArray bigData; // several megabytes of data to insert in DB
> const int chunkSize = 50000;
> 
> 
> int i = 0;
> while(!bigData.isEmpty()) {
>   QByteArray chunk = bigData.left(chunkSize);
>   bigData = bigData.mid(chunkSize);
> 
> 
>   QString str = QString::fromLatin1("UPDATE t SET data=CONCAT(data, 0x
> %1) WHERE id=42").arg(QString(chunk.toHex()));
> 
> 
>   QSqlQuery q2(db);
>   q2.prepare(str);
>   q2.exec(); //<<<<<< still fails when i==max_allowed_packet/chunkSize
> 
> 
>   i++;
> }
> 
> 
> 
> 
> Note that this technique works fine when using phpmyadmin or any other
> mysql interactive kind of console, which proves that the BLOB data
> doesn't get mangled by CONCAT.
> Anybody has an idea? Bug in QtSql?

What happens if data is NULL on the first update? In that case, CONCAT
would also return NULL. This is safer:

  QString str = QString::fromLatin1("UPDATE t "
                 "SET data=CONCAT(COALESCE(data,''), 0x%1) WHERE id=42")
                 .arg(QString(chunk.toHex()));






More information about the Qt-interest-old mailing list