I m working in a project with a Postres DB. I m trying to delete all rows of a table based on a python list.
My code is :
listofMonths = []
listofMonths = pl.month.unique() # list of months in which there is data to update
deletequery = f'DELETE FROM {table_name} WHERE month IN (' + ','.join((str(n) for n in listofMonths)) + ');'
db.execute(deletequery)
I have an error : Deleted error into table plc (psycopg2.errors.UndefinedFunction) operator does not exist: text = integer LINE 1: DELETE FROM plc WHERE month IN (2020-03-0... ^ HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
month column is a text one.
my query seems to be like that : 'DELETE FROM plclass123145722922162 WHERE month IN (2020-03-01,2020-01-01,2020-02-01,2020-04-01);'
I m sure that I need ' between values but don t know to fix that
thanks
You can enclose n
in a pair of single quotes when outputting it:
deletequery = f'DELETE FROM {table_name} WHERE month IN (' + ','.join(f"'{n}'" for n in listofMonths) + ');'