Robert Robert - 1 month ago 5
SQL Question

Postgresql: How to delete rows from table constrained by date?

I am using psycopg2 and how can I delete rows that are older than a certain date? For example:

cursor.execute('DELETE FROM datatable WHERE date < %s', datetime.date(2012, 1, 1))


If I write like this, there will be TypeError: 'datetime.date' object does not support indexing. How can I do with it? Thanks!

Answer

Pass the second argument as a list or a tuple:

cursor.execute('DELETE FROM datatable WHERE date < %s', [datetime.date(2012, 1, 1)])