The problem is you are doing dynamic sql, your sql is one big string. So, apostrophes need to be doubled when you want them.
Similar to:
query = 'SELECT * FROM table1 WHERE field1 IN(''' + 19800 + ''')...
You'll have to get your apostrophes all lined up.