C++ storing custom object as blob in Mysql -
i need store c++ object mysql database blob. following code,
int mysqldb::save(string name, trackerfeatures features) { ofstream ofs("features.ros", ios::binary); ofs.write((char*)&features, sizeof(features)); try{ sql::preparedstatement *pstmt = con->preparestatement("insert faces(name,features) values(?,?)"); istream stream(ofs.rdbuf()); pstmt->setstring(1, name); pstmt->setblob(2, &stream); pstmt->executeupdate(); } catch (sql::sqlexception &e) { cout << "# err: sqlexception in " << __file__; cout << "(" << __function__ << ") on line " << __line__ << endl; cout << "# err: " << e.what(); cout << " (mysql error code: " << e.geterrorcode(); cout << ", sqlstate: " << e.getsqlstate() << " )" << endl; } return exit_success; }
when pass data function, see features.ros file being created binary data. also, there row created in table "features" column empty. not sure getting wrong.
the raw buffer of std::ofstream instance (named ofs) empty when constructing std::istream object using raw buffer.
however, it's not idea store actual memory representation of given object, reason... instead, consider creating proper persisting methods, gives valid , portable representation, desired classes.
Comments
Post a Comment