开发者社区> 问答> 正文

因为双引号不能更新postgres DB中的字典列吗?

我可以用两个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中运行命令,做一些类似子进程的事情。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,但是我不能安装任何东西。 问题来源StackOverflow 地址:/questions/59385942/cant-update-dictionarys-column-in-postgres-db-because-of-double-quotes

展开
收起
kun坤 2019-12-25 22:11:38 436 0
1 条回答
写回答
取消 提交回答
  • 你应该能够反斜杠转义内部双引号:

    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
    

    你还应该能够做一个heredoc直接到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_
    
    2019-12-25 22:11:44
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载