Serializing objects in C ++ and saving as blob type in mysql

I am using the mysql / C ++ connector to connect to the mysql database. I have several complex data structures, so I need to serialize them and store them in the database.

I tried something like the following.

vector<int> vectorTest(10,100);
istream *blob = NULL;
ostringstream os;
int size_data = sizeof(vector<int>);

blob = new istringstream((char*)&vectorTest, istringstream::in | istringstream::binary);
string qry = "INSERT INTO vector(id,object) VALUES (?,?)";

prep_stmt = con->prepareStatement(qry);

prep_stmt->setInt(1,1);
prep_stmt->setBlob(2,blob);
prep_stmt->execute();

I just tried a small example here. However, the vector object is not saved.

Alternatively, I can use the following approach.

ostringstream os;
int size_data = sizeof(vector<int>);
os.write((char*)&vectorTest, size_data);

However, I do not know how to redirect the output stream to the input stream, because the setBlob () method requires istream as the input parameter.

May I learn how to make any of these examples work? If my approach is wrong, can someone provide some sample code or improve this segment of code? Your immediate reply is very welcome.

thank

+3
3

. "", , , - ​​ . , :

struct vector_int {
  unsigned int num_elements;
  int* elements;
};

elements - , .

, , num_elements, elements. , , , , . std::vector, , , .

"" , . , , . , . , , , , , , , .

, int :

1413 1812 1 219 4884 -57 12 

, , .

:

vector<int> vectorTest(10,100);
ostringstream os;

for (vector<int>::const_iterator i = vectorTest.begin(); i != vectorTest.end(); ++i) 
{
  os << *i << " ";
}

// Then insert os.str() into the DB as your blob

:

// Say you have a blob string called "blob"

vector<int> vectorTest;
istringstream is(blob);
int n;

while(is >> n) {
  vectorTest.push_back(n);
}

, , , , , - . , , , int. , , .

+6

MySQL Visitor . BLOB, ():

struct Field
{
    // Every field has a name.
    virtual const std::string    get_field_name(void) = 0;

    // Every field value can be converted to a string (except Blobs)
    virtual const std::string    get_value_as_string(void) = 0;

    // {Optional} Every field knows it SQL type.
    // This is used in creating the table.
    virtual unsigned int         get_sql_type(void) = 0;

    // {Optional} Every field has a length
    virtual size_t               get_field_length(void) = 0;
};

, , bool . Field SQL INSERT SELECT.

A Record . for_each() :

struct Field_Functor
{
    virtual void    operator()(const Field& f) = 0;
};

struct Record
{
    void    for_each(Field_Functor& functor)
    {
      //...
      functor(field_container[i]);  // or something similar
    }
};

Visitor, SQL . - . Field get_field_name get_value_as_string.

struct Field_Integer;

struct Visitor_Base
{
    virtual void process(const Field_Integer& fi) = 0;
    virtual void process(const Field_String& fs) = 0;
    virtual void process(const Field_Double& fd) = 0;
};


struct Field_With_Visitor
{
    virtual void    accept_visitor(Visitor_Base& vb) = 0;
};

struct Field_Integer
{
    void    accept_visitor(Visitor_Base& vb)
    {
        vb.process(*this);
    }
};

The record using the `Visitor_Base`:
struct Record_Using_Visitor
{
    void accept_visitor(Visitor_Base& vistor)
    {
        Field_Container::iterator   iter;
        for (iter = m_fields.begin();
             iter != m_fields.end();
             ++iter)
        {
            (*iter)->accept_visitor(rv);
        }
        return;
    }
};

BLOB- MySQL ++ Connector wxWidgets.

: MySQL database .

+1

boost ( )

XML JSON

0
source

Source: https://habr.com/ru/post/1743843/


All Articles