Results 1 to 13 of 13

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

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