Results 1 to 7 of 7

Thread: Read image and save to 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 Read image and save to database?

    I am using the following code to take screenshot and saving it in the hard disk, i want to read the file and save it into the blob field of the mysql database. How to connect to the particular database,table and field. Please help me with my code.

    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 

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read image and save to database?

    Use QSqlQuery::prepare() and QSqlQuery::bindValue() to insert blob's.

    Why do you use QODBC driver and not the native QMYSQL???
    A camel can go 14 days without drink,
    I can't!!!

  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: Read image and save to database?

    I am a beginner, I just want to take screenshots and open the database and save the image's blob data into the field. Could you please help me in modifying my code in order to achieve this. This application connects the server database of the office. Will it be possible. please help me giving the modified code of my block .. thank you

  4. #4
    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: Read image and save to database?

    I have tried using the native QMYSQL also. but it failed to connect.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Read image and save to database?

    It is plain that you are expecting someone here to your job for you, and you are clearly disappointed that this has not happened. It should be apparent that no-one here is going to write the complete solution for you.

    Your truly appalling grasp of SQL has been corrected before, as has nearly every single aspect of this problem. This thread repeats the general answer for accessing BLOB fields from databases. You have been given direct links to the relevant pages of the Qt documentation on the problem of manipulating a file in-memory without a temporary file on disc. You have been given nudges in the right direction. You have been given links to open source project that do similar things. We simply cannot help someone that cannot be bothered to take the very advice that they are asking for.

    How to take screenshots and save the binary data in the blob field? points you back to numerous previous instances of asking for, and utterly ignoring, advice on this same problem. You need to build and install the MySQL database plugin before you can connect to a MySQL database using the native driver. You have been told this before but clearly haven't bothered.

  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: Read image and save to database?

    You are good in researching others work flow.

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Read image and save to database?

    ... and you, Sir, are very good at ignoring all advice and continuing to ask the same question yet again (resurrecting a two year old thread for no reason) with no evidence of any effort on your part to take on advice already offered.

    Here is the summary:
    • Stop using obsolete, deprecated or unsafe methods, e.g. QString::sprintf(), when better alternatives exist
    • Use QBuffer and QByteArray to save an image to an in-memory buffer (no temporary disk file).
    • Learn some basic SQL. If you cannot construct a basic SQL query outside Qt/C++ then you will not have any luck with QSqlQuery.
    • Use QSqlQuery::prepare() and QSqlQuery::bindValue() to insert blob's. In fact, you should use this anywhere you are tempted to build a query from many parts into a string.
    • You need to build and use the MySQL native driver to access your database directly rather than through ODBC.
    Last edited by ChrisW67; 22nd July 2011 at 00:40.

  8. The following user says thank you to ChrisW67 for this useful post:

    Gokulnathvc (22nd July 2011)

Similar Threads

  1. how to save a image file ( say png ) into mysql database?
    By AviMittal in forum Qt Programming
    Replies: 12
    Last Post: 21st July 2011, 12:49
  2. how to save database
    By khalid_se in forum Newbie
    Replies: 7
    Last Post: 10th July 2011, 18:47
  3. How to save the QColor and then read it?
    By ding in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2011, 10:55
  4. read files and save the,
    By kernel_panic in forum Qt Programming
    Replies: 2
    Last Post: 4th January 2007, 06:31
  5. Save images to database
    By jnk5y in forum Qt Programming
    Replies: 4
    Last Post: 8th May 2006, 19:56

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.