0
def copy_sql_db(storage_connection, mysql_connection, date):
    cursor = storage_connection.cursor()
    mysql_cursor = mysql_connection.cursor()

    if date == "ALL":
      cursor.execute("""select name, test_time, id from test_table""")
    else:
      cursor.execute("""select name, test_time, id from test_table where test_time > (CURDATE() - INTERVAL date day)""")
    ...

I do not know what to search for to get the variable date that I pass into my function, copy_sql_db into my else: cursor.execute...when I run the script created I get there is an error in the SQL syntax near where test_time > date.

user20929302
  • 383
  • 1
  • 4
  • 14

1 Answers1

0

You need to use a placeholder

try this:

    cursor.execute("""select name, test_time, id from test_table where test_time > (CURDATE() - INTERVAL {0} day)""".format(date))

or SAFE:

    cursor.execute("""select name, test_time, id from test_table where test_time > (CURDATE() - INTERVAL %s day)""", (date,))
Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127
Dataichou
  • 317
  • 1
  • 6