postgresql - Why does psql not recognise my single quotes? -


$ psql -e --host=xxx --port=yyy --username=chi --dbname=c_db -c 'delete "stock_profile" "symbol" = 'msft'; ' 

error: column "msft" not exist line 1: delete "stock_profile" "symbol" = msft;

how show psql msft string?

it not 'msft', \'msft\' or ''msft''

the problem have you've run out of types of quote mark nest; breaking apart, have:

  1. your shell needs pass single string psql command; can either single quotes or double quotes
  2. your table name mixed case needs double quoted
  3. your string needs single quoted

in example give:

psql -e --host=xxx --port=yyy --username=chi --dbname=c_db -c 'delete "stock_profile" "symbol" = 'msft'; ' 

the shell sees 2 single-quoted strings:

  • 'delete "stock_profile" "symbol" = '
  • `'; '

so problem not in psql, in shell itself.

depending on shell using, single-quoted strings don't accept escapes (so \' doesn't help) double-quoted strings do. therefore try using double-quotes on outer query, , escaping them around table name:

psql -e --host=xxx --port=yyy --username=chi --dbname=c_db -c "delete \"stock_profile\" \"symbol\" = 'msft'; " 

now \" won't end string, shell see single string:

"delete \"stock_profile\" \"symbol\" = 'msft'; " 

and pass psql escapes processed, resulting in desired sql:

delete "stock_profile" "symbol" = 'msft';  

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -