Results 1 to 20 of 20

Thread: How to save the image blob data into the database??

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default How to save the image blob data into the database??

    I am using the following code:
    I am reading the jpeg image in Qbytearray; But while saving to database, the image size is doubled and while retrieving from database, the file is being corrupted. How to read the image properly??

    Qt Code:
    1. QByteArray buf=NULL;
    2. originalPixmap =QPixmap();
    3. originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
    4. QString strfname;
    5. strfname.sprintf("%d",c);
    6. originalPixmap.save("C:\\Program Files\\image"+strfname+".jpeg","jpeg");
    7. c++;
    8. char Data;
    9. QFile file("C:\\Program Files\\image"+strfname+".jpeg");
    10. file.open(QIODevice::ReadOnly);
    11. int len=file.size();
    12. //buf=file.read(len);
    13. buf=file.readAll();
    14. file.close();
    15. file.remove();
    16. QSqlQuery query;
    17. query.exec("INSERT INTO log (grab_date, ip_address, image,logged_user) "
    18. "VALUES (?,?,?,?)");
    19. query.bindValue(0,dd);
    20. query.bindValue(1,address);
    21. query.bindValue(2,buf.toBase64());
    22. query.bindValue (3,hostname);
    23. bool qry=query.exec();
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to save the image blob data into the database??

    please post in one of your 1214234 other thread on this topic.

    please stop creating new threads to the same topics again and again.

  3. #3
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to save the image blob data into the database??

    Please try to fix my problem

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to save the image blob data into the database??

    Learn what base64 is and how it works. And stop spawning threads on the same subject, this is really annoying.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: How to save the image blob data into the database??

    You're saving the data in base64. As I understand it, there should be no need to do that if the field is defined as a blob.

    If you saved it in base64, though, you need to translate it out of base64 on retrieval. Eg, use QByteArray.fromBase64.

  6. #6
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to save the image blob data into the database??

    If i save the image just as QByteArray, How it should be retrieved??

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to save the image blob data into the database??

    The same way, as a byte array. Just make sure the database doesn't perform any conversions when you store the data.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Read image and save to database?
    By Gokulnathvc in forum Newbie
    Replies: 6
    Last Post: 22nd July 2011, 00:18
  2. Replies: 10
    Last Post: 30th June 2011, 22:47
  3. Replies: 4
    Last Post: 16th June 2011, 14:49
  4. Replies: 2
    Last Post: 17th February 2010, 14:32
  5. Replies: 1
    Last Post: 14th September 2009, 08:48

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
  •  
Qt is a trademark of The Qt Company.