Hi, i'm currently loading images from a sqlite3 blob-field using this code:
void ImageViewer::loadImage ( int pic )
{
query.prepare ( "SELECT data FROM img_images WHERE id = :id" );
query.bindValue ( ":id", img_list[pic] );
query.exec();
if ( query.next() )
{
return QImage::fromData ( query.
value ( 0 ).
toByteArray() ) );
}
else
{
}
}
void ImageViewer::loadImage ( int pic )
{
QSqlQuery query ( QSqlDatabase::database() );
query.prepare ( "SELECT data FROM img_images WHERE id = :id" );
query.bindValue ( ":id", img_list[pic] );
query.exec();
if ( query.next() )
{
return QImage::fromData ( query.value ( 0 ).toByteArray() ) );
}
else
{
return QImage();
}
}
To copy to clipboard, switch view to plain text mode
Now i found some examples in one of my Qt-Books:
QImage ImageCollection
::getImage( int id
) {
qry.prepare ( "SELECT data FROM images WHERE id = :id" );
qry.bindValue ( ":id", id );
if ( !qry.exec() ) qFatal ( "Failed to get image" );
if ( !qry.next() ) qFatal ( "Failed to get image id" );
return image;
}
QImage ImageCollection::getImage( int id )
{
QSqlQuery qrx;
qry.prepare ( "SELECT data FROM images WHERE id = :id" );
qry.bindValue ( ":id", id );
if ( !qry.exec() ) qFatal ( "Failed to get image" );
if ( !qry.next() ) qFatal ( "Failed to get image id" );
QByteArray array = qry.value ( 0 ).toByteArray();
QBuffer buffer ( &array );
buffer.open ( QIODevice::ReadOnly );
QImageReader reader ( &buffer, "PNG" );
QImage image = reader.read();
return image;
}
To copy to clipboard, switch view to plain text mode
Which code is better, if i know that i only have one image format in the database and why? which one is the fastest?
Using Qt4.4b1, 4.4rc1 is compiling atm. System is Debian Sid.
C167
Bookmarks