Skip to content

Insert nested arrays into sql from python

I have a list contains many lists in python.

The nested list at index 0 contains the column headers, and rest of the nested lists contain corresponding values. How would I insert this into sql server using pyodbc or slqalchemy? I have been using pandas pd.to_sql and want to make this a process in pure python. Any help would be greatly appreciated.

expected output table would look like:

Advertisement

Answer

Since the column names are coming from your list you have to build a query string to insert the values. Column names and table names can’t be parameterised with placeholders (?).

Update:

If you have a large number of records to insert you can do that in one go using executemany. Change the code like this:

User contributions licensed under: CC BY-SA
3 People found this is helpful