由于双引号,无法更新 postgres 数据库中字典的列?
Can't update dictionary's column in postgres DB because of double quotes?
我可以在两个 CLI 命令中更新有问题的列:
psql -U postgres
UPDATE taskt SET update= '[{"id": 0, "timestamp": "2019-12-17T22:16:28.985Z", "statusUpdate": "three little piggies"}]' WHERE uri = '/rest/tasks/xyz';
效果很好,但我打算 运行 执行 python 中的命令并执行类似 subprocess.check_output(cli_0 + " & " + cli_1, shell=True) 导致认为第二个是 bash 命令。理想情况下,我想要 运行 类似的东西:
psql -U postgres -c "UPDATE taskt SET update= '[{"id": 0, "timestamp": "2019-12-17T22:16:28.985Z", "statusUpdate": "three little piggies"}]' WHERE uri = '/rest/tasks/xyz';"
但是字典中的双引号有点扼杀了这个想法。将它们变成单引号也不起作用。任何建议都会非常有帮助。我可以访问 python3.5,但无法通过 pip 安装任何东西。
您应该能够对内部双引号进行反斜杠转义:
psql -U postgres -c "UPDATE taskt SET update= '[{\"id\": 0, \"timestamp\": \"2019-12-17T22:16:28.985Z\", \"statusUpdate\": \"three little piggies\"}]' WHERE uri = '/rest/tasks/xyz';"
否则,您可以保存到文件并运行它:
cat <<_EOF_ > /tmp/myfile
> UPDATE taskt SET update= '[{"id": 0, "timestamp": "2019-12-17T22:16:28.985Z", "statusUpdate": "three little piggies"}]' WHERE uri = '/rest/tasks/xyz';
> _EOF_
psql -U postgres < /tmp/myfile
你也应该能够直接在 psql
:
中做一个 heredoc
psql postgres postgres <<_EOF_
> select 1;
> _EOF_
?column?
----------
1
(1 row)
psql U postgres <<_EOF_
UPDATE taskt SET update= '[{"id": 0, "timestamp": "2019-12-17T22:16:28.985Z", "statusUpdate": "three little piggies"}]' WHERE uri = '/rest/tasks/xyz';
_EOF_
我可以在两个 CLI 命令中更新有问题的列:
psql -U postgres
UPDATE taskt SET update= '[{"id": 0, "timestamp": "2019-12-17T22:16:28.985Z", "statusUpdate": "three little piggies"}]' WHERE uri = '/rest/tasks/xyz';
效果很好,但我打算 运行 执行 python 中的命令并执行类似 subprocess.check_output(cli_0 + " & " + cli_1, shell=True) 导致认为第二个是 bash 命令。理想情况下,我想要 运行 类似的东西:
psql -U postgres -c "UPDATE taskt SET update= '[{"id": 0, "timestamp": "2019-12-17T22:16:28.985Z", "statusUpdate": "three little piggies"}]' WHERE uri = '/rest/tasks/xyz';"
但是字典中的双引号有点扼杀了这个想法。将它们变成单引号也不起作用。任何建议都会非常有帮助。我可以访问 python3.5,但无法通过 pip 安装任何东西。
您应该能够对内部双引号进行反斜杠转义:
psql -U postgres -c "UPDATE taskt SET update= '[{\"id\": 0, \"timestamp\": \"2019-12-17T22:16:28.985Z\", \"statusUpdate\": \"three little piggies\"}]' WHERE uri = '/rest/tasks/xyz';"
否则,您可以保存到文件并运行它:
cat <<_EOF_ > /tmp/myfile
> UPDATE taskt SET update= '[{"id": 0, "timestamp": "2019-12-17T22:16:28.985Z", "statusUpdate": "three little piggies"}]' WHERE uri = '/rest/tasks/xyz';
> _EOF_
psql -U postgres < /tmp/myfile
你也应该能够直接在 psql
:
psql postgres postgres <<_EOF_
> select 1;
> _EOF_
?column?
----------
1
(1 row)
psql U postgres <<_EOF_
UPDATE taskt SET update= '[{"id": 0, "timestamp": "2019-12-17T22:16:28.985Z", "statusUpdate": "three little piggies"}]' WHERE uri = '/rest/tasks/xyz';
_EOF_