0

I am using Python3 and MySQL. I have a string value in date and time format (dateS).

  import datetime
  dateS = datetime.datetime.strptime(s, '%Y-%m-%d %H:%M:%S')
  # print(dateS)                 # 2018-08-27 00:30:00

I need to delete rows in table "bts_l3_single_value_data" checking with the condition where start_time is equal to mentioned datetime.

  sql = """DELETE FROM bts_l3_single_value_data WHERE start_time = dateS"""

When I execute this I'm getting an error as " Error: (1054, "Unknown column 'dateS' in 'where clause'") " and when I print the sql statement it gives as;

 DELETE FROM bts_l3_single_value_data WHERE start_time = dateS 

without converting the dateS string to datetime format.

Any help. Thanks

Shakya_A
  • 17
  • 4

2 Answers2

2

You are using dateS as a string, if you want to put parameter there better use f strings

sql = f"""DELETE FROM bts_l3_single_value_data WHERE start_time = {dateS}"""

But it's better to use sql variables for more secured.

Đào Minh Hạt
  • 2,742
  • 16
  • 20
0

use .format for inducing value like this in sql query. Below is an example for that

sql = """
         DELETE FROM from bts_l3_single_value_data where start_time ='{}'
       """.format(dateS)
Upasana Mittal
  • 2,480
  • 1
  • 14
  • 19