I made up a quick dummy snippet which is supposed to load a file from the current directory (some png file) and encode it into databse and reload it from there and save it under another name to the ucrrent direcotry:
#include <QtDebug>
#include <QtSql>
#include <QtCore>
#include <QImage>
#include <QApplication>
#include <QWidget>
#include <QVariant>
#include <QByteArray>
#include <QFile>
#include <QBuffer>
int main (int argc, char **argv){
db.setHostName("localhost");
db.setDatabaseName("testdb");
db.setUserName("root");
db.setPassword("abc123"); // not rly
if (!db.open() )
qDebug("Mising db / unable to open");
else {
model.setTable("testtable");
int row = 0;
model.insertRows( row, 1);
img.save(&buffer, "PNG");
model.setData(model.index(row,0),ba.toBase64());
model.submitAll();
QVariant v
(model.
data(model.
index(row,
0)));
// this won't work
if (!f.error())
{
QByateArray ba2 = v.toByteArray();
ba2.toBase64();
f.write(ba2);
f.close();
}
else
qDebug("unable to open file f");
}
// not really reqired, probably totally useless... indeed useless...
app.setApplicationName("halligalli sql");
app.setActiveWindow(a);
a->show();
return app.exec();
}
#include <QtDebug>
#include <QtSql>
#include <QtCore>
#include <QImage>
#include <QApplication>
#include <QWidget>
#include <QVariant>
#include <QByteArray>
#include <QFile>
#include <QBuffer>
int main (int argc, char **argv){
QApplication app(argc,argv);
QImage img("./A.png");
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("testdb");
db.setUserName("root");
db.setPassword("abc123"); // not rly
if (!db.open() )
qDebug("Mising db / unable to open");
else {
QSqlTableModel model;
model.setTable("testtable");
int row = 0;
model.insertRows( row, 1);
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
img.save(&buffer, "PNG");
model.setData(model.index(row,0),ba.toBase64());
model.submitAll();
QVariant v(model.data(model.index(row,0)));
// this won't work
QFile f("./clone.png");
f.open(QIODevice::ReadWrite | QIODevice::Truncate);
if (!f.error())
{
QByateArray ba2 = v.toByteArray();
ba2.toBase64();
f.write(ba2);
f.close();
}
else
qDebug("unable to open file f");
}
// not really reqired, probably totally useless... indeed useless...
QWidget * a = new QWidget();
app.setApplicationName("halligalli sql");
app.setActiveWindow(a);
a->show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Someone can tell me wehere my mistake is?
Bookmarks