Here's my Python code:
import pandas as pd
import psycopg2
# connect to db
conn = psycopg2.connect(
host="localhost",
database="postgres",
user="postgres",
port= "5432",
password="example")
# read in the sql file
fd = open('SQL/example.sql', 'r')
sqlFile = fd.read()
fd.close()
# get query
mydate = '1/1/2021'
data = pd.read_sql(sqlFile, conn)
Here's my SQL script:
select *
from myschema.table
where date = $mydate
I want to pass my Python variable "mydate" to SQL. So when I run SQL, it would show the date stored in the variable as such. i.e. The string sqlFile would actually show:
select *
from myschema.table
where date = '1/1/2021'
How can I do this?