有没有办法将整数数组存储在表的一列中?我想要这样的订单:
ident | value | count
----------------+------------------------------------------------------------------------------------------------------------------------+-------
563 | [0:10]={"(0,0)","(1,100)","(2,200)","(3,300)","(4,400)","(5,500)"} | 6
我已经通过Postgres获得了这一点,但我也希望从SQLITE获得同样的O/P。在这里,列值存储一个数组。我用BLOB试了一下,但不起作用。有人告诉我序列化的方式,但我不确定如何做到这一点。
6条答案
按热度按时间bwitn5fc1#
Sqlite3不直接支持数组。请参见here它支持的类型。基本上,它只处理整数、浮点数和文本。
要实现所需的功能,您必须使用自定义编码,或使用FK,即创建另一个表,其中数组中的每一项都存储为一行。
beq87vna2#
SQLite不支持数组,因此您不能这样存储它们。您可以将数组元素存储为单个字符串,并使用字符串函数或正则表达式将它们解析回其类型,而不是单独存储数组元素。
C#示例:
这会将数组转换为单个字符串,现在将其作为字符串插入。要取回数组,请执行以下操作:
这只适用于一维数组,多维数组在解析字符串时可能会变得很棘手。
rqqzpn5f3#
这是序列化和反序列化数据的一种方式:
无需花费时间解析圆括号和额外的逗号,您可以将其序列化为CSV,并在处理反序列化数据时逐个读取。
yfjy0ee74#
您可以使用JSON.stringify,当您再次读取数据时,可以使用JSON.parse。因此,您可以将数组存储为字符串,并可以轻松地将其再次解析回数组。通过这种方式,还可以将数组的数组放入sqlite db。
ecfdbz9o5#
这是我的设想,尽管它可能是不正确的:
输出格式:
liwlm1x96#
我也在处理同样的问题。我来自JS,但我认为这将是一个跨语言的答案。由于没有人给出Gianni想法的代码示例,而Wolfpack‘08正在询问示例,我将留下我的解决方案。