Reputation: 83
Hello i am trying to load a huge query (5000+ lines) from a text file and execute the whole query at the once using the code below. How can i load my query from a txt file and execute it using python pandas
i opened up my text file using the code below
query1 = open("Script.txt","r")
and tried executing the script with the code below.
df_ora1 = pd.read_sql(query1, con=connection1)
I am getting the following error
DatabaseError: Execution failed on sql '<_io.TextIOWrapper expecting string or bytes object
my text file has """ start query
end query """
Upvotes: 1
Views: 3255
Reputation: 31166
read_sql()
along with connectiontemptable = "tempx"
sql = f"""select Year, count(*) as c
from {temptable}
where Month=1
and Sharpe between 1 and 2
and stock like '%%2%%'
group by Year"""
with open("script.sql","w") as f: f.write(sql)
# read sql script from file. pass it to pandas with connection
with open("script.sql") as f: sql = f.read()
engine = sqlalchemy.create_engine('mysql+pymysql://sniffer:[email protected]/sniffer')
conn = engine.connect()
print(pd.read_sql(sql, conn).to_string(index=False))
output
Year c
2018 930
2019 932
2020 958
Upvotes: 3