在上篇文章中描述了怎樣搭建讀寫數據庫的環境。
本文更進一步,描述了讀寫mySQL數據庫,字符、整型數字、圖片。讀寫圖片相對難點。
數據庫的圖片字段用BLOB,如果圖片較大要用longblob,否則會報錯。
另外,讀寫數據庫都使用了短連接,完成后關閉連接
本文代碼在QT6.2.4 MSVC2019 +MySQL5.7.44_X64 調試通過。
//1.數據庫字段
? ? ? ? ? ? ? 圖1-數據庫字段示意圖
//2.界面
圖2-界面
//3.代碼
//3.1 頭文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QVBoxLayout>
#include <QtSql/qtsql>
QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACE
class Widget : public QWidget
{
? ? Q_OBJECT
public:
? ? Widget(QWidget *parent = nullptr);
? ? ~Widget();
? ? void initUI();//初始化界面
? ? QPushButton* btn_write ;//寫入按鈕
? ? QPushButton* btn_read;//讀取按鈕
? ? QLabel * lb_name;//顯示姓名
? ? QLabel * lb_age;//顯示年齡
? ? QLabel * lb_picture;//顯示圖片
??
private:
? ? Ui::Widget *ui;
? ? QByteArray m_imageData2;//圖片的字節數組
? ? QString m_name;//姓名變量
? ? int m_age;//年齡變量
private slots:
? ? void btn_write_click();//寫入按鈕子程序
? ? void btn_read_click();//讀取按鈕子程序
};
#endif // WIDGET_H
//-----------------------------------------------------------------------------------------------------------
//3.2? cpp文件
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
? ? : QWidget(parent)
? ? , ui(new Ui::Widget)
{
? ? ui->setupUi(this);
? ? initUI();
}
void Widget::initUI()
{
? ? ?QVBoxLayout* vlayout = new QVBoxLayout();
? ? ?btn_write = new QPushButton(this);
? ? ?btn_write->setText("寫入數據庫");
? ? ?btn_write->setFixedWidth(100);
? ? ?vlayout->addWidget(btn_write);
? ? ?btn_read = new QPushButton(this);
? ? ?btn_read->setText("讀數據庫");
? ? ?btn_read->setFixedWidth(100);
? ? ?vlayout->addWidget(btn_read);
? ? ?lb_name=new QLabel("待讀出姓名");
? ? ?lb_name->setFixedWidth(100);
? ? ?vlayout->addWidget(lb_name);
? ? ?lb_age=new QLabel("待讀出年齡");
? ? ?lb_age->setFixedWidth(100);
? ? ?vlayout->addWidget(lb_age);
? ? ?lb_picture=new QLabel("待讀出圖片");
? ? ? vlayout->addWidget(lb_picture);
? ? ?setLayout(vlayout);
? ? QObject::connect(btn_write, &QPushButton::clicked,this,&Widget::btn_write_click);
? ? QObject::connect(btn_read, &QPushButton::clicked,this,&Widget::btn_read_click);
}
void Widget::btn_write_click()
{
? ? qDebug()<<QSqlDatabase::drivers();
? ? QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
? ? db.setHostName("127.0.0.1");
? ? db.setPort(3306);
? ? db.setDatabaseName("db_name");//你自己的數據庫名稱
? ? db.setUserName("root");
? ? db.setPassword("123456");
? ? if (!db.open()) {
? ? ? ? qDebug()<<"不能連接"<<"connect to mysql error"<<db.lastError().text();
? ? ? ? return ;
? ? }
? ? else
? ? {
? ? ? ? qDebug()<<"數據庫連接成功";
? ? }
? ?
? ? QString imagePath = "d:/Pictures/LeiJun.png";
? ? // Read image file
? ? QFile file(imagePath);
? ? if (!file.open(QIODevice::ReadOnly)) {
? ? ? ? qDebug() << "Error: Failed to open image file";
? ? ? ? return ?;
? ? }
? ? QByteArray imageData = file.readAll();
? ? QSqlQuery query;
? ? query.prepare("INSERT INTO test (name, age,image) VALUES (:value1, :value2, :value3)");
? ? query.bindValue(":value1", "LeiJun");
? ? query.bindValue(":value2", 50);
? ? query.bindValue(":value3", imageData);
? ? if (!query.exec())
? ? {
? ? ? ? qDebug() << "Failed to insert data. Error: " << query.lastError().text();
? ? ? ? db.close();
? ? ? ? return ?;
? ? }
? ? qDebug() << "Data inserted successfully";
? ? db.close();
}
void Widget::btn_read_click()
{
? ? qDebug()<<QSqlDatabase::drivers();
? ? QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
? ? db.setHostName("127.0.0.1");
? ? db.setPort(3306);
? ? db.setDatabaseName("db_name");//你自己的數據庫名稱
? ? db.setUserName("root");
? ? db.setPassword("123456");
? ? if (!db.open()) {
? ? ? ? qDebug()<<"不能連接"<<"connect to mysql error"<<db.lastError().text();
? ? ? ? ?db.close();
? ? ? ? return ;
? ? }
? ? else
? ? {
? ? ? ? qDebug()<<"數據庫連接成功";
? ? }
? ?
? ? ?//讀圖片
? ? ?QSqlQuery query;
? ? ? query.exec("SELECT * FROM test WHERE name = 'LeiJun' LIMIT 1;");
? ? if (!query.next()) {
? ? ? ? qDebug() << "Error: Failed" << query.lastError().text();
? ? ? ? ?db.close();
? ? ? ? return ;
? ? }
? ? else
? ? ? ? qDebug() << "讀數據成功";
? ? //姓名
? ? m_name = query.value(0).toString();
? ? qDebug()<<m_name;
? ? lb_name->setText(m_name);
? ? //年齡
? ? m_age = query.value(1).toInt();
? ? qDebug()<<m_age;
? ? lb_age->setText(QString::number(m_age));
? ? //圖片的二進制
? ? m_imageData2 = query.value(2).toByteArray();//轉換成字節數組
? ? // Create image from data
? ? QPixmap pixmap;
? ? pixmap.loadFromData(m_imageData2);
? ? ?lb_picture->setPixmap(pixmap);顯示圖片
? ? db.close();
}
Widget::~Widget()
{
? ? delete ui;
}
//4.代碼下載鏈接
https://download.csdn.net/download/weixin_39926429/89374527