PDA

View Full Version : image saving and retrieving in database using Qt with C++



Khalifa
16th April 2017, 14:00
Creating For All,
I Have an entry form created in Qt for entering and retrieving data of employees, including the employee photo and I have a problem in saving and retrieving the image to and from the database which is SQL server 2008 database, can any one help achieve this goal!!!.

Thanks in advance،،،،،

d_stranz
16th April 2017, 17:15
Convert the picture to QImage if you don't have it already. You can convert a JPG or other file into QImage if you have the photo as a disk file. See the QImage documentation on "Reading and Writing Image Files" to make sure you have the proper plugins for the picture format.

To store in the database, your DB needs to have a BLOB field in the employee record. You can obtain a pointer to the pixels in the image (as unsigned char *) using QImage::constBits() and QImage::byteCount(). You can then store this in your BLOB.

To retrieve from the database, you retrieve the BLOB into an unsigned char array, and pass that and the size into the static QImage::fromData() method. This gives you the same QImage that you stored, unless you or the database did something to mangle the bits.

Khalifa
17th April 2017, 08:48
thank you for your reply, but I'm using SQL Server 2008 database, and I didn't find data type called BLOB, in stead i found 'image' data type, will it do in the place of BLOB??

d_stranz
17th April 2017, 23:02
"BLOB" is an acronym for "binary large object" and is usually a variable-length binary array of bytes. In SQL Server this can be the "binary", "varbinary", "varbinary max", or "image" type. In SQL Server 2008, the "image" data type is deprecated so you should not use it for new work. You should use "varbinary max" instead. This will allow you to store images of up to 2^31 bytes. If you can guarantee that your photos will always be less than 8000 bytes, you can use "varbinary" instead. 8000 bytes is a very small image, so I think I would use "varbinary max". See here. (https://www.connectionstrings.com/sql-server-2008-data-types-reference/)

Urthas
27th April 2017, 20:52
Unless you are constrained to storing the actual images in the database, consider storing their locations (i.e., path or URL), which are just strings.