Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql escape dollar sign

Tags:

postgresql

I've very complex data that I'm inserting into postgresql and am using double dollar ($$) to escape. However I've one row which ends with dollar sign and is causing error. The original row is like 'dd^d\w=dd$' and when escaped '$$dd^d\w=dd$$$'.

How can I escape this specific row?

like image 864
zulqarnain Avatar asked Dec 16 '22 11:12

zulqarnain


2 Answers

Use any string inside the double dollar to differentiate it:

select $anything$abc$$anything$;
 ?column? 
----------
 abc$

The insert is similar:

insert into t (a, b) values
($anything$abc$$anything$, $xyz$abc$$xyz$);
INSERT 0 1
select * from t;
  a   |  b   
------+------
 abc$ | abc$
like image 104
Clodoaldo Neto Avatar answered Dec 18 '22 01:12

Clodoaldo Neto


While Clodoaldo is quite right I think there's another aspect of this you need to look at:

Why are you doing the quoting yourself? You should generally be using parameterised ("prepared") statements via your programming language's client driver. See bobby tables for some examples in common languages. Using parameterised statements means you don't have to care about doing any quoting at all anymore, it's taken care of for you by the database client driver.

I'd give you an example but you haven't mentioned your client language/tools.

like image 40
Craig Ringer Avatar answered Dec 18 '22 01:12

Craig Ringer