0

i am trying to read blob data from sqlite and deserialize it my code

   int init_search(std::string db_path, cv::Mat *voc1, std::list<std::string> *desc_lst)
    {
    sqlite3 *sqdb;
    int rc;
    unsigned char  *bow_data;
    const char *bow_key ;
int bow_size;

cv::Mat bow_desc,desc_tmp;
//const size_t data_size_bow;
rc = sqlite3_open("C:\\aa\\cvdb\\dna.db", &sqdb);
sqlite3_stmt *stmt = NULL;

do 
{
    rc = sqlite3_prepare_v2(sqdb, "SELECT * FROM dna_tab", -1, &stmt, NULL);
    //sqlite3_bind_text(stmt, 3, bow_key, -1, SQLITE_STATIC);
    rc = sqlite3_step(stmt);
    if (rc == SQLITE_ROW)
    {
        bow_size = sqlite3_column_bytes(stmt, 3);
        bow_data = (unsigned char *)malloc(bow_size);
        //memcpy(bow_data, sqlite3_column_blob(stmt, 3), bow_size);
        //std::stringstream s(bow_key,std::ios_base::in |std::ios_base::binary);
        std::stringstream s((const char *)sqlite3_column_blob(stmt, 3), std::ios_base::binary);


        boost::archive::binary_iarchive  oa(s, std::ios_base::binary);
        oa >> bow_desc;

        cout << bow_desc;
    }
    rc = sqlite3_finalize(stmt);


} while (rc == SQLITE_SCHEMA);
sqlite3_close(sqdb);
return 0;
}

and here is mat object load and save code, i am able to save it to sqlite blob but getting unhandled exception while trying to deserialize

     BOOST_SERIALIZATION_SPLIT_FREE(::cv::Mat)
 namespace boost {
   namespace serialization {

    /** Serialization support for cv::Mat */
    template <class Archive>
        void save(Archive& ar, const ::cv::Mat& m, const unsigned int version)
        {
        size_t elem_size = m.elemSize();
        size_t elem_type = m.type();

        ar & m.cols;
        ar & m.rows;
        ar & elem_size;
        ar & elem_type;

        const size_t data_size = m.cols * m.rows * elem_size;
        ar & boost::serialization::make_array(m.ptr(), data_size);
        }

    /** Serialization support for cv::Mat */
    template <class Archive>
        void load(Archive& ar, ::cv::Mat& m, const unsigned int version)
        {
        int cols, rows;
        size_t elem_size, elem_type;

        ar & cols;
        ar & rows;
        ar & elem_size;
        ar & elem_type;

        m.create(rows, cols, elem_type);

        size_t data_size = m.cols * m.rows * elem_size;
        ar & boost::serialization::make_array(m.ptr(), data_size);
        }

      }
   };

Please help...

  • I can't help you with this, but you can look [here](http://stackoverflow.com/a/32357875/5008845) how to (de-) serialize an image (the example is on a iostream, but you can easily adapt, I guess). – Miki Dec 04 '15 at 12:05
  • 1
    why not simply drop the whole boost nonsense, and write rows,cols,type, data to your sqlite db ? – berak Dec 04 '15 at 13:31
  • @miki - example is with file stream which is by default accepted by istream, but i am getting error while passing a streangstream and i guess i can't adapt as i am new to CPP and boost – Dipak Mallick Dec 05 '15 at 09:14
  • @DipakMallick so you want your image serialized in a `stringstream`, or `char*`, or what? – Miki Dec 05 '15 at 18:06
  • @miki i think it should be char * as i am getting char * from blob – Dipak Mallick Dec 07 '15 at 07:54

1 Answers1

0

Thanks all after little bit googling solved the issue by defining underflow of custom streambuf

      int underflow()
{

    int rec= sqlite3_blob_read(_blob, buf, max_blob_size, 0);
    if (rec != SQLITE_OK) return traits_type::eof();

    setg(buf, buf, buf + max_blob_size);
    return traits_type::to_int_type(*gptr());
}

it's crude way of overloading streambuf but for the time being my work is done...