delete all records except the id I have in a python list

Posted by jay_t on Stack Overflow See other posts from Stack Overflow or by jay_t
Published on 2010-05-13T11:37:05Z Indexed on 2010/05/13 11:44 UTC
Read the original article Hit count: 168

Filed under:
|

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,

© Stack Overflow or respective owner

Related posts about python

Related posts about mysql