如何向postgres数据库列插入一个包含双引号和三引号的值

时间:2017-01-20 14:03:12

标签: postgresql django-models postgresql-9.3

我想以下列格式将查询字符串插入Postgres数据库列

{"enrolled_time":'''SELECT DISTINCT enrolled_time AT TIME ZONE %s FROM alluser'''}

我试试这个:

UPDATE reports SET raw_query = {"enrolled_time":'''SELECT DISTINCT enrolled_time AT TIME ZONE %s FROM alluser'''}  WHERE id=37;

它给出了错误,如

ERROR:  syntax error at or near "{"
LINE 1: UPDATE base_reports SET extra_query = {"enrolled_time":'''SE...

当我尝试使用单引号时,会抛出如下错误:

ERROR:  syntax error at or near "SELECT"
LINE 1: ...DATE reports SET raw_query = '{"enrolled_time":'''SELECT DIS...

我如何克服这种情况

1 个答案:

答案 0 :(得分:1)

使用dollar quoting

UPDATE reports
SET raw_query = $${"enrolled_time":'''SELECT DISTINCT enrolled_time AT TIME ZONE %s FROM alluser'''}$$
WHERE id = 37;