I have a dictionary with keys and values like:
my_dict = {'a':33, 'b': 'something', 'c': GETDATE(), 'd': 55}
Assume column names in the SQL table are also named like the keys of the dict, i.e. "a,b,c,d".
The actual dictionary is 20+ key:value pairs.
Code
I have used pyodbc.connect to create a cursor
which I could use to execute an SQL INSERT statement:
for k in my_dict.keys():
cursor.execute(
'''
INSERT INTO TABLEabc (%s)
VALUES (%s)
'''
% (k, my_dict[k])
)
This seems inefficient though because it's a new SQL operation each time.
- What is the easiest way to insert the values using a loop?
- How could I write it so that it just makes one insert with all the values?