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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.