Skip to content Skip to sidebar Skip to footer

How Do I Use Executemany To Write The First Values In Each Key To A Database

I am new to Python and am trying to use a for loop to add the first and subsequent values, sequentially from a list within a dictionary to a database by using the cursor.executeman

Solution 1:

executemany wants a list of tuples each one containing one row, that is, one element from yo, one from meh, and one from blah. That is not what .items() provide, since it will provide a key and the values associated with that key on each iteration.

Luckly the zip() function does exactly what you need:

cur.executemany('...insert...', zip(my_dict['yo'], my_dict['meh'], my_dict['blah']))

Post a Comment for "How Do I Use Executemany To Write The First Values In Each Key To A Database"