Results 1 to 4 of 4

Thread: Read BLOB from MySQL database

  1. #1
    Join Date
    Jul 2010
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Read BLOB from MySQL database

    I want to read the BLOB field in my table to reconstruct the image save there my problem is that can't retrieve the value of BLOB from db to (bytes) which is (QByteArray).

    This is my code:
    Qt Code:
    1. void Dialog::RetrieveFromDatabase()
    2. {
    3. QString imagePath = QFileDialog::getSaveFileName(this,"Save image to",QDir::currentPath(),"JPEG Image (*.jpg)");
    4.  
    5. QSqlQuery query;
    6. query.prepare("SELECT image FROM `test`.`images` WHERE `id_no`=':id';");
    7. query.bindValue(":id",ui->lineID->text());
    8. if (!query.exec())
    9. {
    10. QMessageBox::critical(this,"Query error",query.lastError().text());
    11. }
    12. else
    13. {
    14. //Load image form database
    15. QByteArray bytes = query.value(query.record().indexOf("image")).toByteArray();
    16.  
    17. //Save image to disk
    18. QImage imageWrite;
    19. imageWrite.loadFromData(bytes,"JPG");
    20. imageWrite.save(imagePath,"JPG");
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2010
    Location
    Sudan
    Posts
    46
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Read BLOB from MySQL database

    see this link ... it contain example that may help you

    or u can use this code :

    Qt Code:
    1.  
    2. query.exec("select image FROM images where id_no=1;");
    3. model.setQuery(query);
    4.  
    5. index = model.index(0,0);
    6.  
    7. QByteArray ba1 = index.data().toByteArray();
    8. QPixmap pic;
    9. pic.loadFromData( ba1);
    10.  
    11. pic.save(imagePath,"JPG");
    To copy to clipboard, switch view to plain text mode 
    Last edited by tinysoft; 22nd May 2011 at 21:19.

  3. The following user says thank you to tinysoft for this useful post:

    zero-n (22nd May 2011)

  4. #3
    Join Date
    Jul 2010
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Read BLOB from MySQL database

    my problem is that (bytes) is empty after running this function but i don't know why.

    i have another function that saved images into my database that works correctly, and i am pretty sure that there is no problem with MySQL database.

  5. #4
    Join Date
    Jul 2010
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Read BLOB from MySQL database

    Thanks for tinysoft now my code is working well.

    Qt Code:
    1. void Dialog::RetrieveFromDatabase()
    2. {
    3. QString imagePath = QFileDialog::getSaveFileName(this,"Save image to",QDir::currentPath(),"JPEG Image (*.jpg)");
    4. QModelIndex index;
    5. QByteArray bytes;
    6.  
    7. QSqlQuery query;
    8. query.prepare("SELECT image FROM `test`.`images` WHERE `id_no`=:id;");
    9. query.bindValue(":id",ui->lineID->text());
    10. if (!query.exec())
    11. {
    12. QMessageBox::critical(this,"Query error",query.lastError().text());
    13. }
    14. else
    15. {
    16. //Set model
    17. model.setQuery(query);
    18. index = model.index(0,0);
    19. bytes = index.data().toByteArray();
    20.  
    21. //Save image
    22. QImage imageWrite;
    23. imageWrite.loadFromData(bytes);
    24. imageWrite.save(imagePath,"JPG");
    25.  
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to read and write a float* to mySQL as a BLOB?
    By agerlach in forum Qt Programming
    Replies: 1
    Last Post: 20th May 2011, 08:54
  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. Replies: 2
    Last Post: 17th February 2010, 14:32
  5. problem with read blob from postgres db
    By zlatko in forum Qt Programming
    Replies: 4
    Last Post: 5th November 2006, 08:30

Tags for this Thread

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.