代码之家  ›  专栏  ›  技术社区  ›  Manfred Singer

将字典(字符串,Int16())保存到PostgreSQL单元格中

  •  1
  • Manfred Singer  · 技术社区  · 8 年前

    Dictionary(of String, INT16()). 该字典表示测量的模拟值。我想将这个字典存储到PostgreSQL数据库的一个单元格中。我正在使用Npgsql。

    我搜索了很长时间,但我不知道该怎么做。

    我的想法是序列化字典并将其保存为数组,但我不确定这是否正确。

    谢谢你的回答

    3 回复  |  直到 8 年前
        1
  •  1
  •   Alessandro Da Rugna    8 年前

    JSON datatype ,甚至应用 functions 在上面。

        2
  •  0
  •   Shay Rojansky    8 年前

    hstore . 缺点是hstore只存储字符串,因此不会键入int。

        3
  •  -1
  •   Slime recipe    8 年前

    看一看 CREATE TYPE

    CREATE TYPE StringIntPairType AS (k string,v int);
    

    更新:

    CREATE TYPE StringIntPairType AS (k text,v int);
    

    创建示例表。请注意,集合列的末尾标记为“[]”

    CREATE TABLE CollectionsTables
    (
      Id SERIAL UNIQUE,
      Collection  StringIntPairType[],
      Description TEXT,
      TS TIMESTAMP NOT NULL DEFAULT now()
    )
    

    INSERT INTO CollectionsTables(Collection,Description) VALUES
    (
        array
        [
           ('Test1',1)::StringIntPairType,
           ('Test2',2)::StringIntPairType
        ],
        'Description1'
    );
    

    INSERT INTO CollectionsTables(Collection,Description) VALUES
            (
                array
                [
                   ('TestA',30)::StringIntPairType,
                   ('TestB',40)::StringIntPairType
                ],
                'Description2'
            );
    

    注意,这是一个常规表,与任何其他表一样

    SELECT * FROM CollectionsTables
    

    这里有一些更高级的选择。

    SELECT Collection[1].k,Description FROM CollectionsTables