PDA

View Full Version : Insert image (PNG) into MySQL table (longblob)



graciano
1st April 2014, 18:06
Hi,
I'm calling primeInsert(int,QSqlRecord&) in order to set initial values for a QTableView/QSqlTableModel approach (not sure about this:o).


void Dialog::primeInsertImage(int row, QSqlRecord &record)
{
record.setValue("id", generateNextId("object")); // To get the next free id
record.setValue("name", ui->fileNameLabel->text()); // To get a QString from a QLabel
QImage currentImage = ui->imageLabel->pixmap()->toImage();
QByteArray bytes;
QBuffer buffer(&bytes);
buffer.open(QIODevice::WriteOnly);
currentImage.save(&buffer, "PNG");
record.setValue("image", bytes); //To get the image previously loaded into a QLabel
}


The insert code is:


void Dialog::on_InsertRecord_clicked()
{
view->setFocus();
int row = model->rowCount();
model->insertRows(row,1);
QModelIndex index = model->index(row, model->fieldIndex("id"));
view->setCurrentIndex(index);
view->edit(index);
}


It works fine until i press enter (change record in the view).

Then i only get one fiel with data (The id).
The "name" and "image" fiels are NULL!

What could i be doing wrong?

Ginsengelf
2nd April 2014, 09:36
Hi, just a guess (I have never used QSqlRecord):
the docs say "When queries are generated to be executed on the database only those fields for which isGenerated() is true are included in the generated SQL."
So maybe your data is not sent to your database?

Ginsengelf

graciano
2nd April 2014, 18:04
Good point Ginsengelf!

I checked the value of isGenerated() and it was "false" for all fields.

I just added:


record.setGenerated("id", true);
record.setGenerated("name", true);
record.setGenerated("image", true);


Now it sends all data to the database.
I still don't get why the primary key ("id") was sent anyway!

Thanks