Bottom Page

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()
micseydel wrote Apr-02-2019, 06:11 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Larz60+ wrote Apr-01-2019, 10:06 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

You have already been asked to use BBCode tags. Please read forum rules. Thank You
Quote
#2
You need to provide the full stack trace. There's often essential information in it (likely the case here).
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  pyodbc.Error SQLBindParameter pcarra 0 240 Jul-08-2019, 08:22 PM
Last Post: pcarra
  Pyodbc error taxit 1 509 Jun-18-2019, 01:13 AM
Last Post: Larz60+
  Using VBA to Call a Python script causes error in pyodbc connector pcarra 1 391 Jun-11-2019, 04:14 PM
Last Post: pcarra
  Using executemany to import the data Sandy7771989 1 248 Jun-11-2019, 07:45 AM
Last Post: Sandy7771989
  MySQL executemany - Failed executing the operation; Could not process parameters josh_marchant 1 584 May-16-2019, 04:22 PM
Last Post: woooee
  Split List and Sublist from Pyodbc parthi1705 1 310 May-05-2019, 10:44 AM
Last Post: Larz60+
  Read CSV file into MySQL and use executemany instead of execute bluethundr 1 600 May-04-2019, 07:35 AM
Last Post: MvGulik
  Validating information from .csv file before executemany mzmingle 7 499 Apr-15-2019, 01:40 PM
Last Post: mzmingle
  PyODBC error - second parameter to executemany must be a sequence, iterator, or gener RBeck22 1 920 Mar-29-2019, 06:44 PM
Last Post: RBeck22
  Pyodbc does not accept variable from tkinter diegoctn 9 586 Feb-12-2019, 05:12 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)