Home > Blockchain >  How to SQL DELETE a python generated list
How to SQL DELETE a python generated list

Time:03-02

I am not strong in SQL at all, and I tried searching around on how to convert my Python list into a proper format SQL. But I can only seem to convert into single separated tuples, instead of having all strings in one tuple.

Users_to_del = ['Aske Meyer', 'UserT', 'BUser', 'test3']
del_string1 = ', '.join([f"(\'{name}\')" for name in users_to_del])
sql =  'DELETE FROM users_from_group WHERE name_of IN %s;' % del_string1
print(sql)
cur.execute(sql)

The SQL query:

DELETE FROM users_from_group WHERE name_of IN ('Aske Meyer'), ('UserT'), ('BUser'), ('test3');

What I want to achieve:

DELETE FROM users_from_group WHERE name_of IN ('Aske Meyer', 'UserT', 'BUser', 'test3');

CodePudding user response:

Take the brackets out of the join:

del_string1 = ', '.join([f"\'{name}\'" for name in users_to_del])
sql =  'DELETE FROM users_from_group WHERE name_of IN ( %s );' % del_string1
  • Related