0

I have SQL queries in some data that looks includes single quotes, e.g. SELECT * from TABLE where date = '2022-11-11' this is causing issues when inserting into a table because of single quotes.

I'm using Python to process the data coming in but replacing the ''' with '\'' doesn't seem to be working. I thought escaping the single quotes but would work but queries to INSERT still fail. What would be the best way to handle inserting single quote SQL queries into a VARCHAR column?

Paul
  • 1,101
  • 1
  • 11
  • 20

1 Answers1

1

Double single quotes are used to escape single quote in Presto/Trino:

select 'SELECT * from TABLE where date = ''2022-11-11''';

Output:

_col0
SELECT * from TABLE where date = '2022-11-11'

So you can format your query correspondingly

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • This now fails on multi-line queries. Is there a way to handle long multiline queries? All I can think of is splitting it into an array of strings.. – Paul Nov 11 '22 at 23:47
  • @Paul have you tried anything from [here](https://stackoverflow.com/q/10660435/2501279)? – Guru Stron Nov 12 '22 at 04:58
  • 2
    I ended up replacing all `\n` with `\t` and it works now. – Paul Nov 12 '22 at 05:01