MySQL saves images + Qt reads and reads images from the database
I still remember that I had to save images to the database in the previous project, and Qt had to read and read the images in the database. After a long time, I checked a lot of information to solve the problem, so I hope this article will help you. Now let's talk about the implementation steps.
1. store images in MySQL
First, declare the field type as longblob when creating the table, as shown below:
create table `sfood`(`name` varchar(255) not null,`type` varchar(255) not null,`material` varchar(255) not null,`price` int(200) not null,`feature` varchar(255) not null,`image` longblob,primary key(`name`))ENGINE=innodb default charset=gb2312;
Image is my image field, declared as longblob type, indicating the image of food.
Insert data to the table:
Insert into sfood (name, type, material, price, feature, image) values ('raw cabbage ', 'sichuan food', 'cabbage, raw Water', 8, 'light ', LOAD_FILE ('G: \ images \ chuancai \ baicai.jpg '));
Here, LOAD_FILE ('G: \ images \ chuancai \ baicai.jpg ') is used to write images to the image field. Here, an absolute path is used to indicate the position of your image. This is in windows. In Linux, change the directory interval //.
In this way, we have already written images to the database.
2. How to read images from the database in Qt? The following code uses the above table sfood as an example:
QString select = "select * from sfood";
query.exec(select);
if( query.next() )
{
QLabel *PicLabel = new QLabel();
QPixmap photo;
Photo. loadFromData (query. value (5 ). toByteArray (), "JPG"); // read the image as binary data from the database. The image format is JPG and then displayed in the QLabel.
PicLabel->setPixmap(photo);
PicLabel->setScaledContents(true);
}
3. Write images to the database through Qt
Query.exe c ("select * from sfood where name = '" + nameEdit-> text () + "'"); // The code here is to add dishes, query whether the dish exists by name
if(query.next())
{
QMessageBox: information (this, tr ("warning"), tr ("this dish is already stored in the database "));
db.Close();
return;
}
query.prepare("insert into sfood(name,type,material,price,feature,image) values(?,?,?,?,?,?)");
query.addBindValue(nameEdit->text());
query.addBindValue(typeEdit->text());
query.addBindValue(materialEdit->toPlainText());
query.addBindValue(priceEdit->text());
query.addBindValue(featureEdit->text());
// The following code is to save the image to the database.
ImagePath. replace ("\", "/"); // convert the path format. imagePath is the path of the image file. Here I use the absolute path.
/* The imagePath method can be written as follows:
imagePath = QFileDialog::getOpenFileName(this, tr("Open File"),
"/home",
tr("Images (*.jpg)"));
*/
QByteArray bytes;
QBuffer buffer(&bytes);
buffer.open(QIODevice::WriteOnly);
pictureLabel->pixmap()->save(&buffer,"JPG");
QByteArray data;
QFile * file = new QFile (imagePath); // file is the binary data file name.
file->open(QIODevice::ReadOnly);
data = file->readAll();
file->close();
QVariant var(data);
query.addBindValue(var);
query.exec();
OK. The image has been written to the database through Qt.
I don't have any skills. I hope I can help the cainiao that I need just the same. I also hope that some senior engineers will give me advice or have better methods.