Python TypeError - MySQL executemany

I am using the MySQLdb module to insert into my database. I have the following code

      sqlString = """INSERT INTO PERFORMER (PERFORMER)
                     VALUES (%s)
                     WHERE NOT EXISTS (SELECT * FROM PERFORMER P
                                       WHERE P.PERFORMER = %s)"""
      data = [(c, c) for c in self._performers]
      self._cursor.executemany(sqlString, data)

Basically I want to insert records from self._performers(this is just a list of names) that are not already in the table PERFORMERS. But I get the following TypeError

TypeError: not all arguments converted during string formatting

Full trace

Traceback (most recent call last):
File "tvGuide.py", line 437, in <module>
        processing.UpdatePerformers()
File "tvGuide.py", line 307, in UpdatePerformers
        self._cursor.executemany(sqlString, data)
File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 246, in executemany
        self.errorhandler(self, TypeError, msg)
File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
        raise errorclass, errorvalue TypeError: not all arguments converted during string formatting

The list of self._performers is very long, but here is a sample of the first few entries

['Lucy Alexander', 'Martin Roberts', 'Alistair Appleton', 'Zak Bagans', 'Aaron Goodwin', 'Nick Groff', 'John Zaffis', 'Ellen Pompeo', 'Patrick Dempsey',
+4
source share
1 answer

+1 for Nacho, TypeError has been slightly redone, it is illegal to use WHERE with the VALUES clause.

0
source

Source: https://habr.com/ru/post/1535998/


All Articles