【发布时间】:2017-06-16 16:39:04
【问题描述】:
我正在运行一个非常大的数据集,其中每一行都有一个 json 对象。在经过近 10 亿行之后,我的交易突然出现错误。
我正在使用 Python 3.x 和 psycopg2 来执行我的事务。
我要写的json对象如下:
{"message": "ConfigManager: Config if valid JSON but doesn't seem to have the correct schema. Adopting new config aborted."}
并且 psycopg2 报告以下内容:
syntax error at or near "t"
LINE 3: ...": "ConfigManager: Config if valid JSON but doesn't seem to ...
我知道问题是单引号,但是我不知道如何逃避这个问题,以便可以将 json 对象传递到我的数据库。
我的 json 列的类型为 jsonb,名为 first_row
所以如果我尝试:
INSERT INTO "356002062376054" (first_row) VALUES {"message": "ConfigManager: Config if valid JSON but doesn't seem to have the correct schema. Adopting new config aborted."}
我仍然收到错误消息。我似乎没有做任何事情来解决这个问题。我尝试用\ 转义t 并将双引号更改为单引号,并删除大括号。没有任何效果。
我相信我记得做这件事很重要
`json.dumps({"message": "ConfigManager: Config if valid JSON but doesn't seem to have the correct schema. Adopting new config aborted."})` in my python code and yet I am getting this problem.
【问题讨论】:
-
您必须转义
'并将值加上引号。或使用不同的引号,如$not_sgl_quote$或更好的准备语句,然后传递值 -
使用
$似乎不起作用 -
例如:
INSERT INTO "356002062376054" (first_row) VALUES e'{"message": "ConfigManager: Config if valid JSON but doesn\'t seem to have the correct schema. Adopting new config aborted."}' -
我的错:
INSERT INTO "356002062376054" (first_row) SELECT e'{"message": "ConfigManager: Config if valid JSON but doesn\'t seem to have the correct schema. Adopting new config aborted."}' -
或:
INSERT INTO "356002062376054" (first_row) values (E'{"message": "ConfigManager: Config if valid JSON but doesn\'t seem to have the correct schema. Adopting new config aborted."}');
标签: python json postgresql syntax-error psycopg2