'Python script to query database with Where Clause Date using Pandas

The Situation:

I need to query an Oracle Database using a python script. The query must select or filter a date column. So there is a where clause.

The code

startdate = '2018-01-01'
sql = "SELECT * FROM MY_TABLE WHERE MY_DATE >= ?"
cursor_oracle = connection_oracle.cursor()
df_ora = pd.read_sql(sql, connection_oracle, params=startdate)

The Problem:

I've been hacking at this all day, and I get so many different errors:

  • Date parameter, but expecting int,
  • SQL Execution bad termination..

How do I simply query a database table with a where clause where the column is a date field and I need to specify a date value?

Do I need to convert my date value to a date object? So - what I mean by that is:

date_to_be_queried = "2020-01-01"
date_to_be_queried_object = datetime.someConversionFunction(date_to_be_queried).

I've reached my wits-end.

The error message:

the current error message (the new kid on the block) is:

pandas.io.sql.DatabaseError: Execution failed on sql 'SELECT * FROM MY_TABLE WHERE MY_DATE >= ?': ORA-01036: illegal variable name/number

clues:

the field type of the MY_DATE column is DATE an example of a typical entry in this column is:

2002-03-28 00:00:00



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source