escape single quotes in sql query while passing as variable to python

23 Views Asked by At

I am trying to query athena using php. My code is as below which works.

PHP

<?php
$sql2Py = "'SELECT address,order,status from myDB.order;'";
$output = shell_exec("python3 pythonScript.py $sql2Py");
echo "my result-" . $output;
?>

pythonScript.py

#!/usr/bin/env python
import os
import sys
from pyathena import connect
import pandas

sql = str(sys.argv[1])
orders = pandas.read_sql(sql,conn) 
print (orders)

How do I escape the quotes while adding a string to the query? I tried using backslash like below but this does not work.

$sql2Py = "'SELECT address,order,status from myDB.order where status <> \'Completed\';'";
0

There are 0 best solutions below