delete all records except the id I have in a python list
- by jay_t
Hi all,
I want to delete all records in a mysql db except the record id's I have in a list. The length of that list can vary and could easily contain 2000+ id's, ...
Currently I convert my list to a string so it fits in something like this:
cursor.execute("""delete from table where id not in (%s)""",(list))
Which doesn't feel right and I have no idea how long list is allowed to be, ....
What's the most efficient way of doing this from python?
Altering the structure of table with an extra field to mark/unmark records for deletion would be great but not an option.
Having a dedicated table storing the id's would indeed be helpful then this can just be done through a sql query... but I would really like to avoid these options if possible.
Thanks,