MySQL存入图片+Qt读入读出数据库中的图片_MySQL
还记得之前的一个项目里要向数据库中存入图片,然后Qt要在数据库中读入读出图片,当时纠结了好久,查阅了很多资料才解决,所以希望本文能给需要朋友点帮助。好了废话不多说,下面开始讲实现步骤。
1.MySQL存入图片
首先建表时要声明字段的类型为longblob类型,如下:
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就是我的图片字段,声明为longblob类型,表示食物的图片。
然后往表中插入数据:
insert into sfood(name,type,material,price,feature,image) values('生水白菜','川菜','白菜,生水',8,'清淡',LOAD_FILE('G:\\images\\chuancai\\baicai.jpg'));
这里LOAD_FILE('G:\\images\\chuancai\\baicai.jpg')的作用就是往image字段写入图片,这里用的是绝对路径,表示你图片所在的位子。这是在windows下,如果是在Linux下,要把目录间隔改成//。
这样我们就已经在数据库里写入了图片了。
2.在Qt里如何把图片从数据库里面读出来,接下来的代码都是以上面的表sfood为例:
QString select = "select * from sfood";
query.exec(select);
if( query.next() )
{
QLabel *PicLabel = new QLabel();
QPixmap photo;
photo.loadFromData(query.value(5).toByteArray(), "JPG"); //从数据库中读出图片为二进制数据,图片格式为JPG,然后显示到QLabel里
PicLabel->setPixmap(photo);
PicLabel->setScaledContents(true);
}
3.通过Qt往数据库中写入图片
query.exec("select * from sfood where name='"+nameEdit->text()+"'"); //我这里本段代码是添加菜品,该句是查询是否有该菜,按名字查询
if(query.next())
{
QMessageBox::information(this,tr("警告"),tr("该菜已在数据库存储了"));
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());
//接下来代码是保存图片到数据库
imagePath.replace("\\","/"); //转换路径格式,imagePath是图片文件的路径,我这里用的是绝对路径
/*imagePath的获得方法可以这样写:
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为二进制数据文件名
file->open(QIODevice::ReadOnly);
data = file->readAll();
file->close();
QVariant var(data);
query.addBindValue(var);
query.exec();
ok,已经通过Qt将图片写入数据库了。
没什么技巧,希望可以帮到跟我一样需要的菜鸟,也期望有师兄指教错误或者是有更好的方法。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In PowerPoint, it is a common technique to display pictures one by one, which can be achieved by setting animation effects. This guide details the steps to implement this technique, including basic setup, image insertion, adding animation, and adjusting animation order and timing. Additionally, advanced settings and adjustments are provided, such as using triggers, adjusting animation speed and order, and previewing animation effects. By following these steps and tips, users can easily set up pictures to appear one after another in PowerPoint, thereby enhancing the visual impact of the presentation and grabbing the attention of the audience.

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

1. Open the slide and insert all pictures. 2. Click Picture Tools | Format | Picture Layout and select the hexagon cluster. 3. Then type the text content in the text box on the left. 4. Select the image and the system will activate the SmartArt tool menu. 5. Click the Design menu, then find the SmartArt style toolset, and then click the Change Color button. 6. Select the color you want in the pop-up theme color drop-down menu. 7. The effect after the design is completed is as shown in the figure below:

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.
