How to store array in one column in Sqlite3?

后端 未结 4 984
日久生厌
日久生厌 2020-12-09 14:44

Is there any way to store an array of integers in one column of table? I want o/p like this:

ident | value                                                            


        
4条回答
  •  温柔的废话
    2020-12-09 15:23

    This is one way of serializing and deserializing data:

    #include 
    #include 
    #include 
    #include 
    
    std::vector deserialize_array(std::string const &csv)
    {
      std::istringstream parse(csv);
      std::vector ret;
      for(std::string token; std::getline(parse, token, ','); ret.push_back(token));
      return ret;
    }
    
    std::string serialize_array(std::string* array_ptr, std::size_t N)
    {
      std::ostringstream cat;
      for(std::size_t index= 0; index< N; ++ index)
        cat<< array_ptr[index]<< ',';
      std::string ret= cat.str();
      return ret.substr(0, ret.size()-1);
    }
    
    int main()
    {
      std::string data= "1,2,3";
      std::cout<< "Data: "<< data<< std::endl;
      std::vector deserialized= deserialize_array(data);
      std::string serialized= serialize_array(deserialized.data(), deserialized.size());
      std::cout<< "Serialized + Deserialized: "<< serialized<< std::endl;
    }
    

    Instead of spending time parsing parentheses and extra commas, you can serialize as csv and read two by two when processing the deserialized data.

提交回复
热议问题