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

6.3k Views Asked by At

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!

1

There are 1 best solutions below

0
On BEST ANSWER

Pass the second argument as a list or a tuple:

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