Incorrect number of values

Viewed 45

I am getting the following error: CMySQLCursor.execute() takes from 2 to 4 positional arguments but 5 were given. I get the concept, but cannot work out where the error is in my code:

for row in word_cloud_data.itertuples():
  mycursor = mydb.cursor()
  mycursor.execute('''
                    INSERT INTO dbo.word_cloud (word, word_frequency, Results_file)
                    VALUES (?,?,?)
                    ''',
                    row.word,
                    row.word_frequency,
                    row.Results_file
                    )
sql_db.commit()
2 Answers

Based on this, you have to "specify the variables using %s or %(name)s" and give them over to the function using a tuple or dictionary.

In your case, this should do the trick

for row in word_cloud_data.itertuples():
  mycursor = mydb.cursor()
  mycursor.execute('''
                    INSERT INTO dbo.word_cloud (word, word_frequency, Results_file)
                    VALUES (%s,%s,%s)
                    ''',
                    (row.word, row.word_frequency, row.Results_file)
                  )
sql_db.commit()

You need to pass parameters in tuple.

mycursor.execute('''...''',
                 (row.word,
                  row.word_frequency,
                  row.Results_file)
                )
Related