auato
auato

Reputation: 33

Python help: difficulty to pass the day date variable to sql

Even though there are similar questions here I've tried over and over unsuccessfully so far.

My python script charges into the variable "code" all the lines of the sql from sql.txt:

start_time = input("insert[dd-mm-yyyy]:")
code = open(r'Query\sql.txt', 'r').read()
final = cursor.execute(code).fetchall()

In sql.txt the sql code is performed by OPENQUERY statement:

SELECT * FROM OPENQUERY(GATEWAY,'SELECT * FROM meas_table WHERE daytime >= to_date(''13-08-2020 00:00'', ''dd-mm-yyyy HH24:MI'') ')

How can I change the sql line to pass correctly the variable start_time got from the python script to the sql code?

Upvotes: 1

Views: 51

Answers (1)

mustafasencer
mustafasencer

Reputation: 773

I would format the query (string formatting).

Accept the start_time variable to format your query.

SELECT * 
FROM OPENQUERY(GATEWAY,'SELECT * FROM meas_table WHERE daytime >= to_date('{date_here}', ''dd-mm-yyyy HH24:MI'') ')
code.format(date_here=start_time)

Upvotes: 1

Related Questions