Python Forum
Problem with pyodbc executemany()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with pyodbc executemany()
#1
Well, here goes again, I am trying in vain to insert multiple rows to SQLServer using pyodbc executemany. the data is in a dict.
When I do this I get
#pyodbc.ProgrammingError: The second parameter to executemany must be a sequence, iterator, or generator.

I can't imagine why a dict would not be iterable, I mean what's the point of it? I have tried converting the dict to a dataframe, no go there. Tried switching from Python3.7 to 3.6, no go there! Can't find anyone to contact at pyodbc, anyone have any idea why this thing does not work, has anybody gotten it to work with executemany? Is there another package I can use?

Anyway sorry for the verbosity any help appreciated!

Here is what Iam using
import requests
from requests.auth import HTTPDigestAuth
import json
from pandas.io.json import json_normalize
import pandas as pd
from pandas import ExcelWriter
import random
import pyodbc
def getDev():

   
    url="https://blahblah/v1/devstuff" 
    headers = {"content-type": "application/json", "Authorization": "Bearer sadsadsad"}
    requester = requests.get(url, headers=headers)   
    dict3 = requester.json() #Convert to JSON 
connStr= pyodbc.connect('DRIVER={SQL Server};Server=nnnn;Trusted_Connection=True;DATABASE=aaaa')
    stmt = connStr.cursor()
    for key, value in dict3.items():
        for idx, x in enumerate(value):            
            myDict["remotecontrol_id"], idx
            myDict["device_id"],idx 
            myDict["alias"], idx
            myDict["groupid"], idx
            myDict["online_state"], idx 
stmt.executemany("""INSERT INTO dbo.DevReport(remotecontrol_id, device_id, alias,groupid,
    online_state)) 
    values(?, ?, ?, ?, ?, ?, ?)""", myDict) #Error:Must be sequence, iterator, or generator.

    connStr.commit()
    cursor.close()
    connStr.close()
getDev()
Reply
#2
You need to provide the full stack trace. There's often essential information in it (likely the case here).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using pyodbc&pandas to load a Table data to df tester_V 3 747 Sep-09-2023, 08:55 PM
Last Post: tester_V
  Command error - cursor.executemany(comandoSQL,valoresInserir) TecInfo 2 1,302 Nov-18-2022, 01:57 PM
Last Post: TecInfo
  pyodbc gmerritt 8 2,799 Feb-21-2022, 07:21 PM
Last Post: gmerritt
  MERGE SQL in oracle executemany kaladin 0 3,042 Oct-12-2020, 11:48 AM
Last Post: kaladin
  Formatting Data/Time with Pyodbc and openpyxl bearcats6001 0 2,251 Aug-17-2020, 03:44 PM
Last Post: bearcats6001
  Get database used data space from pyodbc susja 1 2,201 Aug-14-2020, 02:01 PM
Last Post: susja
  pyodbc error ('82', '[82] 523 80 (0) (SQLDriverConnect)') paulsuk1982 1 2,134 Nov-29-2019, 11:05 AM
Last Post: Larz60+
  pyodbc.Error SQLBindParameter pcarra 0 3,843 Jul-08-2019, 08:22 PM
Last Post: pcarra
  Pyodbc error taxit 1 5,139 Jun-18-2019, 01:13 AM
Last Post: Larz60+
  Using VBA to Call a Python script causes error in pyodbc connector pcarra 1 2,775 Jun-11-2019, 04:14 PM
Last Post: pcarra

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020