问题描述
$ psql -E --host=xxx --port=yyy --username=chi --dbname=C_DB -c 'DELETE FROM "Stock_Profile" WHERE "Symbol" = 'MSFT'; '
错误:列 msft不存在
第1行:从 Stock_Profile中删除 WHERE Symbol = MSFT;
ERROR: column "msft" does not existLINE 1: DELETE FROM "Stock_Profile" WHERE "Symbol" = MSFT;
如何显示psql MSFT是字符串?
How do I show psql that MSFT is a string?
它不喜欢'MSFT',\'MSFT\'或'MSFT'
It does not like 'MSFT', \'MSFT\' or ''MSFT''
推荐答案
您遇到的问题是您已经用完了很多引号,无法嵌套;分开,我们有:
The problem you have is that you've run out of types of quote mark to nest; breaking apart, we have:
- 您的shell需要将单个字符串传递给
psql
命令;这可以是单引号或双引号 - 您的表名是大小写混合的,因此需要用双引号
- 您的字符串需要用单引号引起来
- your shell needs to pass a single string to the
psql
command; this can be either single quotes or double quotes - your table name is mixed case so needs to be double quoted
- your string needs to be single quoted
在此示例中,您给出:
psql -E --host=xxx --port=yyy --username=chi --dbname=C_DB -c 'DELETE FROM "Stock_Profile" WHERE "Symbol" = 'MSFT'; '
shell看到两个单引号的字符串:
The shell sees two single-quoted strings:
-
'从 Stock_Profile中删除符号 ='
- `'; ’
所以问题不在于psql,而在于shell本身。
So the problem is not in psql, but in the shell itself.
根据您使用的shell,单引号字符串可能不接受任何转义符(因此 \'
没有帮助),但双引号字符串可能有用。因此,您可以尝试在外部查询上使用双引号,并在表名周围转义它们:
Depending on what shell you are using, single-quoted strings probably don't accept any escapes (so \'
doesn't help) but double-quoted strings probably do. You could therefore try using double-quotes on the outer query, and escaping them around the table name:
psql -E --host=xxx --port=yyy --username=chi --dbname=C_DB -c "DELETE FROM \"Stock_Profile\" WHERE \"Symbol\" = 'MSFT'; "
现在 \
不会结束字符串,因此shell会将其视为单个字符串:
Now the \"
won't end the string, so the shell will see this as a single string:
"DELETE FROM \"Stock_Profile\" WHERE \"Symbol\" = 'MSFT'; "
并将其转义到带有处理的转义的psql中,从而产生所需的SQL:
and pass it into psql with the escapes processed, resulting in the desired SQL:
DELETE FROM "Stock_Profile" WHERE "Symbol" = 'MSFT';
这篇关于为什么psql无法识别我的单引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!