PDA

View Full Version : Save images directly into mysql with qtcreator



Lycus HackerEmo
17th January 2010, 01:32
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.


int MainWindow::captura()
{
QString archivo_foto;
archivo_foto = QFileDialog::getOpenFileName(this,tr("Abrir Archivo"),tr("/home/Imágenes"),tr("Imagenes(*.png *.xpm *.jpg"));
ui->label_foto->setPixmap(archivo_foto);
return 0;
}

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


QSqlQuery query;

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()+"');");

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

numbat
17th January 2010, 08:29
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:


#include <QApplication>
#include <QSqlDatabase>
#include <QDebug>
#include <QSqlQuery>
#include <QSqlError>
#include <QLabel>
#include <QFile>
#include <QByteArray>
#include <QVariant>

QPixmap doSaveAndLoadImage()
{
/* Open an in-memory database. */
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
bool ok = db.open();

QString sql = QString("CREATE TABLE T1(img BLOB)");
QSqlQuery query(db);
ok = query.exec(sql);

QFile file("SomeImage.png");
file.open(QIODevice::ReadOnly);
QByteArray bytes = file.readAll();

query.prepare("INSERT INTO T1(img) VALUES(:img)");
query.bindValue(":img", QVariant(bytes));
ok = query.exec();

query.prepare("SELECT img FROM T1");
ok = query.exec();
ok = query.first();

QPixmap pix;
pix.loadFromData(query.value(0).toByteArray());

return pix;
}




int main(int argc, char * argv[])
{
QApplication a(argc, argv);
QPixmap pix = doSaveAndLoadImage();

QLabel lbl;
lbl.setPixmap(pix);
lbl.show();

return a.exec();
}