Results 1 to 2 of 2

Thread: Save images directly into mysql with qtcreator

  1. #1
    Join Date
    Dec 2009
    Location
    Mexico
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Save images directly into mysql with qtcreator

    Hello good request a little help is that I'm trying to create a database application with the driver qmysql in which I need to save data with all employees and photo (in the same database) for reasons that everything is going to centralize on a remote server and I've searched on google and come in php examples but someone has knowledge about the topic who can give me a help?.

    What they do is the following routines:
    ************************************************** ***********************************
    1 .- capture the image with a digital camera and copied the hard drive.
    2 .- Through a QFileDialog locate the picture, save your route and name in a call QString archivo_foto
    3 .- a QLabel I put in the value setPixmap the path and filename of the image and choose the shows QFileDialog
    the picture on the label.

    Qt Code:
    1. int MainWindow::captura()
    2. {
    3. QString archivo_foto;
    4. archivo_foto = QFileDialog::getOpenFileName(this,tr("Abrir Archivo"),tr("/home/Imágenes"),tr("Imagenes(*.png *.xpm *.jpg"));
    5. ui->label_foto->setPixmap(archivo_foto);
    6. return 0;
    7. }
    To copy to clipboard, switch view to plain text mode 

    But the problem comes when I save the query where I add the second field of this VALUES
    ui-> label_foto-> pixmap () does not know if I miss something or have to convert that into the database field I put as LONGBLOB

    Qt Code:
    1. QSqlQuery query;
    2.  
    3. query.exec("INSERT INTO `ismed`.`empleados` (`id` ,`foto`,`nombre` ,`apellidop` ,`apellidom` ,`puesto` ,`direccion` ,`fecha_ingreso` ,`salario` ,`depto` ,`activo`)VALUES (NULL ,"+ ui->label_foto->pixmap() +",'"+ui->lineEdit_2->text()+"', '"+ui->lineEdit_3->text()+"', '"+ui->lineEdit_4->text()+"', '"+ui->lineEdit_5->text()+"', '"+ui->lineEdit_6->text()+"', '"+ui->lineEdit_7->text()+"', '"+ui->lineEdit_8->text()+"', '"+ui->lineEdit_9->text()+"', '"+ui->lineEdit_10->text()+"');");
    To copy to clipboard, switch view to plain text mode 

    I mark the following error:

    /home/horus/sql/mainwindow.cpp:56: error: invalid operands of types ‘const char [167]’ and ‘const QPixmap*’ to binary ‘operator+’

    thank you very much for your help

  2. #2
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Save images directly into mysql with qtcreator

    You need to learn how to use bound values in sql queries. Not only do you need them for BLOB data, they provide security against sql injection attacks also. Here is a little example of a round trip getting an image in and out of a database:
    Qt Code:
    1. #include <QApplication>
    2. #include <QSqlDatabase>
    3. #include <QDebug>
    4. #include <QSqlQuery>
    5. #include <QSqlError>
    6. #include <QLabel>
    7. #include <QFile>
    8. #include <QByteArray>
    9. #include <QVariant>
    10.  
    11. QPixmap doSaveAndLoadImage()
    12. {
    13. /* Open an in-memory database. */
    14. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    15. db.setDatabaseName(":memory:");
    16. bool ok = db.open();
    17.  
    18. QString sql = QString("CREATE TABLE T1(img BLOB)");
    19. QSqlQuery query(db);
    20. ok = query.exec(sql);
    21.  
    22. QFile file("SomeImage.png");
    23. file.open(QIODevice::ReadOnly);
    24. QByteArray bytes = file.readAll();
    25.  
    26. query.prepare("INSERT INTO T1(img) VALUES(:img)");
    27. query.bindValue(":img", QVariant(bytes));
    28. ok = query.exec();
    29.  
    30. query.prepare("SELECT img FROM T1");
    31. ok = query.exec();
    32. ok = query.first();
    33.  
    34. QPixmap pix;
    35. pix.loadFromData(query.value(0).toByteArray());
    36.  
    37. return pix;
    38. }
    39.  
    40.  
    41.  
    42.  
    43. int main(int argc, char * argv[])
    44. {
    45. QApplication a(argc, argv);
    46. QPixmap pix = doSaveAndLoadImage();
    47.  
    48. QLabel lbl;
    49. lbl.setPixmap(pix);
    50. lbl.show();
    51.  
    52. return a.exec();
    53. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to numbat for this useful post:

    Lycus HackerEmo (18th January 2010)

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. Replies: 4
    Last Post: 27th July 2009, 15:45
  3. Open/Save dialog with images preview
    By mchara in forum Qt Programming
    Replies: 8
    Last Post: 5th November 2007, 07:25
  4. MYSQL save changes
    By eleanor in forum Qt Programming
    Replies: 3
    Last Post: 7th October 2007, 13:27
  5. Save images to database
    By jnk5y in forum Qt Programming
    Replies: 4
    Last Post: 8th May 2006, 19:56

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.