如何在 postgresql 的列内插入括号、括号、冒号、双引号

时间:2021-04-06 11:37:57

标签: sql json postgresql postgresql-9.4

我正在处理应用程序,我需要在列中存储 { [ " : ] } 这些字符。

我该怎么做?

1 个答案:

答案 0 :(得分:4)

这些角色都不需要特别注意:

create table t (col text);
insert into t 
values ('{ [ " : ] }');

但是,如果您打算在该列中存储(有效)JSON 值,最好将其声明为 jsonb 而不是文本:

create table t (col jsonb);
insert into t 
values ('{"key1": 42, "key2": [1,2,3]}');

在 SQL 中的字符串文字中唯一需要特别注意的字符是单引号——它被 doubling it 转义,例如:'Arthur''s house'