okay, i wrote a little testapp that stores an image (or every file you give it) into a BLOB-field. the db-file grows to the size of the given image, so it should be in it, although the chars i get if i do a select to qDebug() are only 8, all unprintable. So, here's my app:
int main( int argc, char *argv[] )
{
db.setDatabaseName( "/home/stefan/source/qt4/cdlauncher/pictures.db" );
if ( !db.open() )
{
qDebug() << "Opening failed";
return 1;
}
qDebug() << "Opening successfull, using database " << db.databaseName();
qDebug() << "filling an image into the DB...";
QFile file( "/home/stefan/pic/space/191853main_image_feature_929_full.jpg" );
if ( file.exists() )
{
{
fileData = file.readAll();
file.close();
}
}
else
{
qDebug() << "File " << file.fileName() << " could not be located";
}
QSqlQuery query
("UPDATE pictures SET data=? WHERE rowid=1;");
query.addBindValue(fileData);
query.exec();
qDebug() << query.lastError();
return 0;
}
int main( int argc, char *argv[] )
{
QCoreApplication app( argc, argv );
QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
db.setDatabaseName( "/home/stefan/source/qt4/cdlauncher/pictures.db" );
if ( !db.open() )
{
qDebug() << "Opening failed";
return 1;
}
qDebug() << "Opening successfull, using database " << db.databaseName();
qDebug() << "filling an image into the DB...";
QFile file( "/home/stefan/pic/space/191853main_image_feature_929_full.jpg" );
QByteArray fileData;
if ( file.exists() )
{
if ( file.open( QIODevice::ReadOnly ) )
{
fileData = file.readAll();
file.close();
}
}
else
{
qDebug() << "File " << file.fileName() << " could not be located";
}
QSqlQuery query("UPDATE pictures SET data=? WHERE rowid=1;");
query.addBindValue(fileData);
query.exec();
qDebug() << query.lastError();
return 0;
}
To copy to clipboard, switch view to plain text mode
that works everything is hardcoded, i know, but its only a test to see how it works.
So, now i want to retrieve the data. the normal way to display an image is
imgLabel->setPixmap(QPixmap::fromImage(QImage("filename")));
To copy to clipboard, switch view to plain text mode
After some testing, reading and coffee-ing, i found the following way:
int main( int argc, char *argv[] )
{
Q_INIT_RESOURCE( application );
db.setDatabaseName( "/home/stefan/source/qt4/cdlauncher/pictures.db" );
if ( !db.open() )
{
qDebug() << "Opening failed";
return 1;
}
QSqlQuery query
( "SELECT data FROM pictures WHERE rowid=1;" );
query.next();
qDebug() << query.lastError();
pic->loadFromData( query.value( record.indexOf( "data" ) ).toByteArray() );
imgLabel->setPixmap( *pic );
wid->show();
return app.exec();
}
int main( int argc, char *argv[] )
{
Q_INIT_RESOURCE( application );
QApplication app( argc, argv );
QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
db.setDatabaseName( "/home/stefan/source/qt4/cdlauncher/pictures.db" );
if ( !db.open() )
{
qDebug() << "Opening failed";
return 1;
}
QSqlQuery query( "SELECT data FROM pictures WHERE rowid=1;" );
QSqlRecord record = query.record();
query.next();
qDebug() << query.lastError();
QByteArray ba = query.value(0).toByteArray();
QWidget *wid = new QWidget();
QLabel *imgLabel = new QLabel( wid );
QPixmap *pic = new QPixmap();
pic->loadFromData( query.value( record.indexOf( "data" ) ).toByteArray() );
imgLabel->setPixmap( *pic );
wid->show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
it successfully reads out the image and displays it...
a SELECT-query seems not to like the QSqlQuery::exec () method. So i used a single next() for that...
So far, everything is fantastic, thank you very much Thomas and jacek
Bookmarks