[odb-users] Storing and retrieving Images using ODB and MySQL

Boris Kolpackov boris at codesynthesis.com
Thu Jul 28 04:27:30 EDT 2011


Hi,

Bright Dadson <losintikfos at yahoo.co.uk> writes:

> Is there any example on storing and retrieving images to MySQL using ODB?

Without saying anything about whether it is a good idea in the first
place, the way to do this is probably to store an image as a BLOB.

To work with BLOBs in the current release (1.5.0) you would need to
provide your own C++ buffer type and also implement the value_traits
specialization for it (see the 'mapping' example for more information
on how to do this).

For some time now I meant to add support for mapping std::vector<char>
to BLOB types and I finally got around to adding this. So if you would
like to use that functionality, you will need to do these things (once
1.6.0 is released these steps will be unnecessary):

1. Get the traits.hxx and traits.cxx files from this commit:

   http://scm.codesynthesis.com/?p=odb/libodb-mysql.git;a=commit;h=e8162bdac61ac49ae30cdfeae9f2bd3d230df809

   And copy them to your libodb-mysql source directory (in odb/mysql/)

2. Re-build and re-install libodb-mysql.

After that, you can do something like this:

#pragma db object
class object
{

  #pragma db type("BLOB")
  std::vector<char> image_;
};

To copy the data into the image, you can do:

object o;

const char* image_bytes = ...
std::size_t image_size = ...

o.image_.assign (image_bytes, image_bytes + image_size);

To read the data from the image, you can do:

object o = ...

const char* image_bytes = o.image_.data ();
std::size_t image_size = o.image_.size ();

Boris



More information about the odb-users mailing list