Results 1 to 13 of 13

Thread: how to save a image file ( say png ) into mysql database?

  1. #1
    Join Date
    May 2009
    Posts
    31
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default how to save a image file ( say png ) into mysql database?

    Hi All

    Is there any way to save image/pdf files into the mysql database. I am Qt4/C++ using QMYSQL plugin.
    I am able to store and retrieve txt files into the database but not any other type of files.

    Please suggest.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to save a image file ( say png ) into mysql database?

    create a BLOB field in MySQL database, then convert needed image in base64 using QByteArray::toBase64 and store it in database. to restore image you need to use QByteArray::fromBase64 and then create an image.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    May 2009
    Posts
    31
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to save a image file ( say png ) into mysql database?

    i tried the same but it didnt worked.

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to save a image file ( say png ) into mysql database?

    it works fine for me. what did you try?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    May 2009
    Posts
    31
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to save a image file ( say png ) into mysql database?

    i am doing it following way..

    say have an image file named: apple.png
    i opened the file and read it completely using file.readAll(). and saved it into QString (str).
    then i converted this QString(str) into QbyteArray and then called toBase64(). and then tried to save it in database and in same way i tried to read it from database but it didnt worked.

    Can you share your sample code for this?

    thanx

  6. #6
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to save a image file ( say png ) into mysql database?

    why are you saving data to QString and then converting it to QByteArray? isn't readAll() alredy returning QByteArray? try to readAll() straight into QByteArray without any QStirings
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  7. #7
    Join Date
    May 2009
    Posts
    31
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to save a image file ( say png ) into mysql database?

    its not working for me

  8. #8
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to save a image file ( say png ) into mysql database?

    can you show us your code?
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  9. #9
    Join Date
    Sep 2009
    Posts
    36
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to save a image file ( say png ) into mysql database?

    Save given QImage directly into QByteArray:

    Qt Code:
    1. QImage image;
    2. QBuffer buffer(&ba);
    3. buffer.open(QIODevice::WriteOnly);
    4. image.save(&buffer, "PNG");
    To copy to clipboard, switch view to plain text mode 
    (Taken from Documentation)

  10. #10
    Join Date
    Oct 2009
    Location
    Rennes, France
    Posts
    20
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to save a image file ( say png ) into mysql database?

    Quote Originally Posted by spirit View Post
    create a BLOB field in MySQL database, then convert needed image in base64 using QByteArray::toBase64 and store it in database. to restore image you need to use QByteArray::fromBase64 and then create an image.
    Why encode QByteArray to base 64?
    BLOB is a binary entry and you can use the QByteArray directely.
    Contributor from the French Qt community from developpez.com
    * Forum
    * FAQ Qt ( > 100 QR)
    * Advanced and Beginner Tutorials

  11. #11
    Join Date
    Sep 2009
    Posts
    36
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to save a image file ( say png ) into mysql database?

    SQL stores blob fields internally as 64bit sized byte arrays, so if you don't convert to base 64, you waste 50% of space, though it shouldn't really make anything work/break which previously worked/was broken....
    Correct me if I am wrong, I am not very familiar with mySQL

  12. #12
    Join Date
    Oct 2009
    Location
    Rennes, France
    Posts
    20
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to save a image file ( say png ) into mysql database?

    Quote Originally Posted by soxs060389 View Post
    SQL stores blob fields internally as 64bit sized byte arrays, so if you don't convert to base 64, you waste 50% of space, though it shouldn't really make anything work/break which previously worked/was broken....
    Correct me if I am wrong, I am not very familiar with mySQL
    Base64 it just a special encoding of binary using 64 char :
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw xyz0123456789+/"
    It's generally use to transfer safely a binary throw network.

    http://fr.wikipedia.org/wiki/Base64

    If you convert a binary to base64 you increase the binary size by 137%.

    But maybe i don't understand something...
    Contributor from the French Qt community from developpez.com
    * Forum
    * FAQ Qt ( > 100 QR)
    * Advanced and Beginner Tutorials

  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 a image file ( say png ) into mysql database?

    I have tried connecting the saving the image to the database, but it fails. i have used the following code for updating the database with the image data:
    Qt Code:
    1. QScreenShot::QScreenShot(QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::QScreenShot)
    4. {
    5. ui->setupUi(this);
    6. c=0;
    7. bool ok=false;
    8. QDateTime dateTime = QDateTime::currentDateTime();
    9. QString dateTimeString = dateTime.toString();
    10.  
    11. QDir().mkdir("D:\\QtImages\\");
    12. QDir().setCurrent("D:\\QtImages\\");
    13. QDir().mkdir("ScreenShots");
    14. QDir().setCurrent("ScreenShots");
    15. QDir().mkdir(dateTimeString);
    16.  
    17. QMessageBox::warning(NULL,"DriveList",dateTimeString,QMessageBox::Ok);
    18.  
    19. QStringList list=QSqlDatabase::drivers();
    20. QString driveList;
    21. for(int i=0;i<list.length();i++)
    22. {
    23. driveList += list[i];
    24. }
    25. //QMessageBox::warning(NULL,"DriveList",driveList,QMessageBox::Ok);
    26. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    27. db = QSqlDatabase::addDatabase("QODBC");
    28. db.setDatabaseName("DRIVER={MYSQL ODBC 3.51 Driver};FIL={MYSQL};DBQ=screengrabber");
    29. db.setHostName("localhost");
    30. db.setConnectOptions("CLIENT_ODBC");
    31. //db.setDatabaseName("screengrabber");
    32. db.setUserName("root");
    33. db.setPassword("1");
    34. ok = db.open();
    35.  
    36. for(;;)
    37. {
    38.  
    39. originalPixmap =QPixmap();
    40. originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
    41. QString strfname;
    42. strfname.sprintf("%d",c);
    43. originalPixmap.save("D:\\image"+strfname+".jpeg","jpeg");
    44. c++;
    45. char Data;
    46. QFile file("D:\\image"+strfname+".jpeg");
    47. file.open(QIODevice::ReadOnly);
    48. file.seek(0);
    49. buf=file.read(250000);
    50. QSqlQuery query;
    51. query.prepare("INSERT INTO log VALUES('grab_date='2011-04-26 15:55:09',ip_address='172.16.0.51',image=:val');");
    52. query.bindValue ( ":val", buf);
    53. QString strquery;
    54. strquery = "INSERT INTO log (grab_date,ip_address,image) VALUES ( '";
    55. strquery += '2011-04-26 15:55:09';
    56. strquery += "' , '";
    57. strquery += '172.16.0.51';
    58. strquery += "' , ";
    59. strquery += buf;
    60. strquery += " );";
    61.  
    62. //QMessageBox::warning(NULL,"DriveList",strquery,QMessageBox::Ok);
    63.  
    64. QSqlQuery insertQuery( strquery, db);
    65. bool done = insertQuery.exec();
    66.  
    67. QFile newfile("D:\\img.txt");
    68. newfile.open(QIODevice::WriteOnly);
    69. newfile.write(buf);
    70.  
    71. int iSecret, iRandom;
    72. iSecret = rand() % 20 + 1;
    73. iRandom=iSecret*60000;
    74.  
    75. QMutex mutex;
    76. mutex.lock();
    77. QWaitCondition waitCondition;
    78. waitCondition.wait(&mutex, iRandom);
    79. mutex.unlock();
    80.  
    81. }
    82.  
    83. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Putting and getting images from MySQL database?
    By allensr in forum Qt Programming
    Replies: 2
    Last Post: 13th May 2007, 21:47
  2. Qt and MySQL Database Connection
    By shamik in forum Qt Programming
    Replies: 41
    Last Post: 6th October 2006, 12:48
  3. Replies: 7
    Last Post: 12th August 2006, 15:11
  4. Issues regarding QMySql drivers and mysql database
    By bera82 in forum Qt Programming
    Replies: 2
    Last Post: 10th August 2006, 17:50
  5. Replies: 8
    Last Post: 7th March 2006, 13:40

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.