将字符串输入 PostgreSQL 会导致单引号出现语法错误
Inputting string into PostgreSQL results in syntax error at single quote
我正在 运行 通过一个非常大的数据集,每一行都有一个 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.
在 SQL.
中,您必须将单引号双引号转义
但是您遇到这个问题意味着您的代码容易受到 SQL injection. You should use parameterized statements 的攻击,并且在执行语句时将字符串作为参数提供。如果你这样做,问题就会消失。
psycopg2
文档有这个可怕但有充分理由的警告:
Warning: Never, never, NEVER use Python string concatenation (+
) or string parameters interpolation (%
) to pass variables to a SQL query string. Not even at gunpoint.
您可以为此使用美元符号约束或 C 样式转义。例如 docs:
An escape string constant is specified by writing the letter E (upper
or lower case) just before the opening single quote, e.g., E'foo'
那么:
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."}');
应该适合你
我正在 运行 通过一个非常大的数据集,每一行都有一个 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.
在 SQL.
中,您必须将单引号双引号转义但是您遇到这个问题意味着您的代码容易受到 SQL injection. You should use parameterized statements 的攻击,并且在执行语句时将字符串作为参数提供。如果你这样做,问题就会消失。
psycopg2
文档有这个可怕但有充分理由的警告:
Warning: Never, never, NEVER use Python string concatenation (
+
) or string parameters interpolation (%
) to pass variables to a SQL query string. Not even at gunpoint.
您可以为此使用美元符号约束或 C 样式转义。例如 docs:
An escape string constant is specified by writing the letter E (upper or lower case) just before the opening single quote, e.g., E'foo'
那么:
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."}');
应该适合你