gpt4 book ai didi

postgresql - 如何通过ansible在postgres查询中转义单引号

转载 作者:行者123 更新时间:2023-11-29 12:03:46 25 4
gpt4 key购买 nike

我能够通过 ansible playbook 执行简单的 postgres 选择/删除查询。但是如果我的查询包含一些单引号,它就会失败。如何转义单引号?

例子

这运行良好:

command: psql -U dbuser dbname -c 'SELECT count(*) from table;'

我想运行这个:

command: psql -U dbuser dbname -c 'SELECT count(*) from table where time <= '01-sep-2016';'

但这给我带来了错误。

最佳答案

不确定它在 ansible playbook 中会如何运作,但通常有 3 种方法来处理这个问题:

  • 在查询周围使用双引号

    command: psql -U dbuser dbname -c "SELECT count(*) from table where time <= '01-sep-2016';"
  • 使用反斜杠:

    command: psql -U dbuser dbname -c 'SELECT count(*) from table where time <= \'01-sep-2016\';'
  • 连续两次使用引号:

    command: psql -U dbuser dbname -c 'SELECT count(*) from table where time <= ''01-sep-2016'';'

关于postgresql - 如何通过ansible在postgres查询中转义单引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39314012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com