4

I am trying to do a batch insert for a single value (row), I am trying to use the executemany function to do so, but it will not work it returns TypeError: not all arguments converted during string formatting. However, when I add an extra value it does work

So... It will return an error here:

entries_list=[("/helloworld"),
                ("/dfadfadsfdas")]
cursor.executemany('''INSERT INTO fb_pages(fb_page)
        VALUES (%s)''', entries_list)

But not here:

entries_list=[("/helloworld", 'test'),
                ("/dfadfadsfdas", 'rers')]
cursor.executemany('''INSERT INTO fb_pages(fb_page, page_name)
        VALUES (%s, %s)''', entries_list)
latonz
  • 1,601
  • 10
  • 21
jped
  • 486
  • 6
  • 19

2 Answers2

4

Writing ("/helloworld") is the same as writing "/helloworld". To create a tuple you need ("/helloworld", ).

What you're doing is actually running this:

cursor.executemany('''INSERT INTO fb_pages(fb_page)
    VALUES (%s)''', ["/helloworld","/dfadfadsfdas"])

And now the error you're receiving makes perfect sense - you're supplying two arguments with only one place holder. Defining entries_list as follows would solve the problem:

entries_list=[("/helloworld",),
            ("/dfadfadsfdas",)]
Korem
  • 11,383
  • 7
  • 55
  • 72
2

In addition to making tuple with an additional , at the end as pointed by Karem

entries_list=[
    ("/helloworld",),
    ("/dfadfadsfdas",)
] 

You could also just pass in lists and that will sort it out for you just as good when you are using parameterized query.

    entries_list=[
      ["/helloworld"],
      ["/dfadfadsfdas"]
]
systemdebt
  • 4,589
  • 10
  • 55
  • 116