将图像作为blob类型插入QT C++中的数据库
inserting an image as a blob type to database in QT C++
从计算机中选择图像后,我正试图将图像作为blob类型插入数据库。我有两个按钮。一种是选择图像。另一个按钮是将图像添加到数据库
选择按钮
// to get an image stored in pc to a qlabel
void EducationSection::on_btnChse_clicked()
{
QString imageFile = QFileDialog ::getOpenFileName(0,"Select Image","/home/","Image Files (*.png)");
QFileInfo info(imageFile);
filename = info.fileName();
QPixmap image (imageFile);
ui->lblBkImge->setPixmap(image);
ui->lblBkImge->show();
}
添加按钮
void EducationSection::on_btnAdd_2_clicked()
{
QByteArray byte;
QFile file(filename);
if(file.open(QIODevice::ReadOnly))
{
byte = file.readAll();
file.close();
}
QMessageBox :: critical(this,"Error",filename);
QSqlQuery query;
query.prepare("insert into eduimage(image) values (:image)");
query.bindValue(":image",byte);
if(query.exec())
{
QMessageBox :: information(this,"Save","Data Inserted successfully", QMessageBox ::Ok);
}
else
{
QMessageBox :: critical(this,"Error","Couldn't insert data");
}
}
这里filename是一个局部变量。一旦选择了图像,它就会根据需要显示在标签中。但数据并没有插入。尽管我试图解决这个问题,但我做不到。请帮帮我。提前谢谢你。
我以前在MySQL数据库中遇到过这个问题。我解决这个问题的方法是明确指定绑定类型必须是原始二进制数据。
在你的情况下,试试
query.bindValue(":image", byte, QSql::In | QSql::Binary);
相关文章:
- Qt VTK交互风格的信号到小部件
- QT在错误的班级中寻找空位
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 如何在Qt C++中更改光标
- 如何在MS Visual Studio 2019中运行QT UI
- QT通过C++添加映射QML项目
- libssh 的函数在构建 libssh 时无法在 Qt 和 cmake 错误中找到
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 使用Qt C++计算类似Git的SHA1哈希
- Qt focusObjectChanged example?
- 如何在cpp文件之间切换窗口?在Qt中
- Qt和C++:将QLineEdit添加到QTabWidget中
- 从数据库实时显示QT c++中的数据
- Qt Quick-如何仅从c++代码与qml属性交互
- 无法使用Qt Creator在Windows中构建yaml-cpp
- 如何在Qt中取消捕获字符串
- Qt SQLite没有查询或参数计数不匹配
- Qt SQlite无法创建表