Results 1 to 20 of 20

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

  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,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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
    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??

    I have tried using base64, still while saving in the database, the file size increases. but the length of the byte array remains same, but saving in the database, it increases


    Added after 1 55 minutes:

    Qt Code:
    1. QFile file("C:\\Program Files\\image"+strfname+".jpeg");
    2. file.open(QIODevice::ReadOnly);
    3. int len=file.size();
    4. buf=file.readAll()
    To copy to clipboard, switch view to plain text mode 

    Here buf contains the blob data, its length is equal to filesize. But while executing the query. its size increases upto 60% and while seeing in database. What's wrong with my code?? Please help me??

    Please dont think me that am re-posting again and again.... Its very hard fro me to find the error. And found nothing in all sites. Please help me to rectify this?
    Last edited by Gokulnathvc; 5th August 2011 at 13:38.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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??

    Quote Originally Posted by Gokulnathvc View Post
    I have tried using base64, still while saving in the database, the file size increases.
    That's totally expected, isn't it?

    Please dont think me that am re-posting again and again....
    We don't have to think. There is a nice button "find all posts of this user" (or alike) so I can see you're spawning many threads on the same problem. Please choose one of the threads and restrict yourself to posting only there.

    Its very hard fro me to find the error. And found nothing in all sites. Please help me to rectify this?
    You need to understand some things. For instance you need to understand why base64 increases volume of the data. For that you need to know how base64 works. If you don't and you're not willing to do the research then don't use it.
    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.


  7. #7
    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??

    I have tried without converting to base64 also, the same problem exists, i just want only the blob data and not any other coded information with it. Could you please help me with my code..

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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??

    There is nothing wrong with the code you posted apart from the fact that you needlessly save the image to a file.
    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.


  9. #9
    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??

    I have used the following code which directly gets the blob data from the pixmap.But in database, blob size increases to about 60%.
    Qt Code:
    1. QByteArray bytes;
    2. QBuffer buffer(&bytes);
    3. buffer.open(QIODevice::WriteOnly);
    4. originalPixmap.save(&buffer, "jpeg");
    To copy to clipboard, switch view to plain text mode 

    For instance, the actual file size is 135 kb but, in database it has been stored as 267 kb.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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??

    And what's wrong with that?
    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.


  11. #11
    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??

    While retrieving the image i cant able to reproduce it, it says the file has been damaged or corrupted.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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??

    How are you retrieving the file? And what's your table schema?
    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.


  13. #13
    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??

    Retrieving the file from database is being done by my colleague which is in C#. He is just getting the bytes from the table and then reproducing it. My table schema is:

    Ip_address(varchar) , grab_date (datetime), image(longblob).

    I am just reading the pixmap and saving the binary data into the QByteArray and then saving it into the database. You can check with the code pasted above..

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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??

    Quote Originally Posted by Gokulnathvc View Post
    Retrieving the file from database is being done by my colleague which is in C#. He is just getting the bytes from the table and then reproducing it.
    Does he do it according to the same rules you use when storing the file? Because if you use base64 and he doesn't then it has no chance of working, you know.
    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.


  15. #15
    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 am saving as a normal image and not as base 64, how could he retreive the image, as it contains some coded information as the size increases?

  16. #16
    Join Date
    Apr 2011
    Posts
    67
    Thanks
    22
    Thanked 5 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

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

    I am a reading this book http://www.apress.com/9781590598313 and although i have not reached the part on saving images,it will help you a little i guess.

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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??

    Quote Originally Posted by Gokulnathvc View Post
    If i am saving as a normal image and not as base 64, how could he retreive the image, as it contains some coded information as the size increases?
    I have no idea what you mean. But the general rule is that if you speak russian and he speaks chinese, you won't understand each other. You need to agree to a specific algorithm and stick with it, both of you. And make sure you store the blob according to MySQL rules, I'm not sure it will accept binary data through a regular textual insert call although it's been very long since I last used MySQL.
    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.


  18. #18
    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.

  19. #19
    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??

  20. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.