Results 1 to 9 of 9

Thread: Qt 5.8 MySQL Input Medium Blob have diffrent size then Output

  1. #1
    Join Date
    Apr 2012
    Posts
    40
    Platforms
    Unix/X11 Windows

    Default Qt 5.8 MySQL Input Medium Blob have diffrent size then Output

    Hi, i have problem with keep binay file in MySQL, when i load file to QByteArray he has size 142652, after insert to MySQL his size is 279514. Why ??
    File type is ".pdf" after load file to disk i cant open this file, i have error.

    Qt Code:
    1. void OrderView::on_pushButtonSave_clicked()
    2. {
    3. QString filePath = QFileDialog::getOpenFileName(this);
    4.  
    5. QByteArray byteArray;
    6. QFileInfo fileInfo(filePath);
    7. QFile file(filePath);
    8. if(file.open(QIODevice::ReadOnly))
    9. {
    10. byteArray = file.readAll();
    11. file.close();
    12. }
    13.  
    14. QSqlQuery query("UPDATE `orders` SET `termsServiceFileName`=?,`termsServiceContent`=? WHERE `orderID`=?");
    15. query.addBindValue( fileInfo.fileName() );
    16. query.addBindValue( byteArray );
    17. query.addBindValue( editOrderID );
    18. query.exec();
    19.  
    20. QMessageBox::critical(this,"", QString::number( byteArray.size() )); //Message show "142652" but in MyPhpAdmin show "[BLOB - 273 KB]" WHY???
    21.  
    22. }
    23.  
    24. void OrderView::on_pushButtonLoad_clicked()
    25. {
    26. QSqlQuery query("SELECT `termsServiceFileName`, `termsServiceContent` FROM `orders` WHERE `orderID`=?");
    27. query.addBindValue( editOrderID );
    28. query.exec();
    29. query.next();
    30.  
    31. QByteArray byteArray = query.value(1).toByteArray();
    32. QMessageBox::critical(this,"", QString::number( byteArray.size() )); //Message show "279514"
    33.  
    34.  
    35. QString filePath = QFileDialog::getSaveFileName(this);
    36. QFile file(filePath);
    37. if(file.open(QIODevice::ReadWrite))
    38. {
    39. file.write( byteArray );
    40. file.close();
    41. }
    42. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt 5.8 MySQL Input Medium Blob have diffrent size then Output

    What is the MySQL datatype for "termsServiceContract"?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Apr 2012
    Posts
    40
    Platforms
    Unix/X11 Windows

    Default Re: Qt 5.8 MySQL Input Medium Blob have diffrent size then Output

    Quote Originally Posted by d_stranz View Post
    What is the MySQL datatype for "termsServiceContract"?
    termsServiceFileName VARCHAR(45)
    termsServiceContract MEDIUMBLOB

  4. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Qt 5.8 MySQL Input Medium Blob have diffrent size then Output

    When you use the QSqlQuery constructor overload for QSqlQuery::QSqlQuery(const QString &query = QString(), QSqlDatabase db = QSqlDatabase()), the SQL statement is executed. I suspect what you want is something similar to:
    Qt Code:
    1. QSqlQuery query(db);
    2. query.prepare("UPDATE orders SET termsServiceFileName=?, termsServiceContent=? WHERE orderID=?");
    3. query.addBindValue(fileInfo.fileName());
    4. query.addBindValue(byteArray);
    5. query.addBindValue(editOrderID);
    6. query.exec();
    To copy to clipboard, switch view to plain text mode 
    Can you give that a try?
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  5. #5
    Join Date
    Apr 2012
    Posts
    40
    Platforms
    Unix/X11 Windows

    Default Re: Qt 5.8 MySQL Input Medium Blob have diffrent size then Output

    Quote Originally Posted by jefftee View Post
    When you use the QSqlQuery constructor overload for QSqlQuery::QSqlQuery(const QString &query = QString(), QSqlDatabase db = QSqlDatabase()), the SQL statement is executed. I suspect what you want is something similar to:
    Qt Code:
    1. QSqlQuery query(db);
    2. query.prepare("UPDATE orders SET termsServiceFileName=?, termsServiceContent=? WHERE orderID=?");
    3. query.addBindValue(fileInfo.fileName());
    4. query.addBindValue(byteArray);
    5. query.addBindValue(editOrderID);
    6. query.exec();
    To copy to clipboard, switch view to plain text mode 
    Can you give that a try?
    Result is the same.

    I foud something interesting. On ".txt" file work properly but on ".jpg" or ".pdf" dont work properly. Maybe when i load data from file "byteArray = file.readAll();" i have to set something like load binary data ??

    Or NO, Because when i load ".jpg" file to QByteArray and save to ".jpg" file without MySQL then work propertly.
    Maybe when i insert QByteArray to MySQL the i have to specify its binary data no text data ??

    When i add "query.addBindValue(byteArray);" MySQL think it is text data, how insert this data as binary data ??
    Last edited by Pablik; 23rd February 2017 at 13:12.

  6. #6
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Qt 5.8 MySQL Input Medium Blob have diffrent size then Output

    Quote Originally Posted by Pablik View Post
    I foud something interesting. On ".txt" file work properly but on ".jpg" or ".pdf" dont work properly. Maybe when i load data from file "byteArray = file.readAll();" i have to set something like load binary data ??
    I suspect your problem is that MySQL is (for some reason) encoding your data in some way, perhaps UTF-8 for example. This shouldn't be happening from what I see regarding the doc for blob data, but the size of your data expands when inserted. When you read it from the database, you get the result that matches the size you see in the actual column, which would indicate that MySQL is simply returning the data to you that it has stored w/o any type of transformation/decoding of the data.

    I suspect this may be a bug with MySQL, because it doesn't appear that blob data should be encoded at all, but that looks like what is happening here...

    One thing you could try and I suspect it would work, would be to base64 encode your data before you insert it and base64 decode it after you read it out the the database.

    Good luck.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  7. #7
    Join Date
    Apr 2012
    Posts
    40
    Platforms
    Unix/X11 Windows

    Default Re: Qt 5.8 MySQL Input Medium Blob have diffrent size then Output

    Quote Originally Posted by jefftee View Post
    One thing you could try and I suspect it would work, would be to base64 encode your data before you insert it and base64 decode it after you read it out the the database.

    Good luck.
    It's work, when i convert QByteArra::toBase64() the size is the same in QByteArra and in MySQL, but when i load from MySQL toBase64() how to convert to orginal QByteArra ??

    OK i found QByteArra::fromBase64()

    Everything work, thx for help

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt 5.8 MySQL Input Medium Blob have diffrent size then Output

    I suspect your problem is that MySQL is (for some reason) encoding your data in some way, perhaps UTF-8 for example.
    This was also my suspicion, and base64-encoding would have been my next suggestion but jefftee has a faster keyboard. Glad this worked.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  9. #9
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Qt 5.8 MySQL Input Medium Blob have diffrent size then Output

    Quote Originally Posted by Pablik View Post
    Everything work, thx for help
    Glad you got it working. The base64 encoding will bloat your data unnecessarily but at least it can survive the round trip into and out of the database! If you have other version(s) of MySQL you can play with, I'd do some testing because I suspect that it's a bug with the version you're using.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. Read BLOB from MySQL database
    By zero-n in forum Newbie
    Replies: 3
    Last Post: 22nd May 2011, 19:59
  2. Inserting blob into mysql using QT?
    By maverick_pol in forum Qt Programming
    Replies: 4
    Last Post: 31st August 2010, 04:37
  3. How to insert BLOB into MySQL with Qt?
    By babygal in forum Qt Programming
    Replies: 3
    Last Post: 26th August 2010, 12:26
  4. How to do logging in a medium size Qt project
    By kumarkun in forum Newbie
    Replies: 2
    Last Post: 18th August 2010, 07:29
  5. Replies: 1
    Last Post: 11th November 2009, 12:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.