I tried to insert the items (using the MySQLdb module) in the list below into mysql table only to find out that the letter 'x' was inserted instead of the names in the list.
name_list=['james', 'john', 'michael']
for x in name_list:
cur.execute("INSERT INTO box(names) VALUES('x')")
db.commit()
Any suggestions? Thanks.
Here's a method that worked, but only for int datatype:
for x in xrange(1,1000,2):
cursor.execute('INSERT INTO Persons (PersonID) VALUES(' + str(x) + ');')
executemanyhere, which is both simpler and potentially faster. You still need to use a placeholder, as in AER's answer, but then you can replace the wholeforloop withcur.executemany("INSERT INTO box(names) VALUES(%s)", [(name,) for name in name_list]). (Notice that I had to create a list of tuples of 1 string each, not just a list of lists, because I think MySQLdb expects each row to be a tuple.)boxtable has anamescolumn in the first place? More generally, when you run your original code, does it actually successfully insert three rows with'x'values? Because if there's a typo in what you posted here, AER will obviously have the same typo in his code…executemanycall", or "insert many by iterating aroundexecute"? Can you post the exact code that you're using for the working and non-working cases (either editing your question, creating a new question with links between them, or posting somewhere like pastebin.com, whichever seems most appropriate)?