sqlite_orm icon indicating copy to clipboard operation
sqlite_orm copied to clipboard

std::vector<std::string> class member

Open ElephantInTheRoomFirst opened this issue 9 years ago • 2 comments

class Vect { std::vectorstd::string _Vec; };

decltype(auto) GetStorage() { return make_storage( "Vector", make_table("vec", make_column("R", &Vect::_Vec) ) ); }

How to store and restore std::vector in and from database?

ElephantInTheRoomFirst avatar Jul 26 '17 19:07 ElephantInTheRoomFirst

This is a good question. First you have to decide how would you store it without sqlite_orm but with pure sqlite3. The problem is that there is no string array type in sqlite or varray just like Oracle DB has. Please check a question here. But you have two options to do a trick.

  1. Store string in a different table with id foreign key per every string. Something similar can be found in subentities.cpp example
  2. Serialize string array somehow on insert. In JSON probably. Parse it on select. There is a very comfortable json lib here.

fnc12 avatar Jul 26 '17 19:07 fnc12

Thanks, I am planning to store it as comma separated strings.

ElephantInTheRoomFirst avatar Jul 26 '17 19:07 ElephantInTheRoomFirst