Pyodbc 错误 "A TVP's rows must be Sequence objects."、'HY000'

Pyodbc Error "A TVP's rows must be Sequence objects.", 'HY000'

我正在调用 API 并将数据从 json 移动到 sql。当我尝试 post 到 sql 我得到这个错误: Pyodbc 错误“TVP 的行必须是序列对象。”,'HY000'

这是我在 python 中的 SQL 声明:

conn =  pyodbc.connect('Driver={ODBC Driver 17 for SQL Server};Server=' + server+ ';Database=' + 
database + ';Trusted_Connection=yes')
cursor = conn.cursor()

for row in range(4):
    cursor.execute('''
            
          INSERT INTO [dbo].[IndustryCode] (id,name,billingAddress,frontOfficeId,link,isArchived,representativeUsers,statusId,status,industry,
                createdDate,lastUpdatedDate,lastActivityDate,latestActivityName,openJobs,street1,street2,city,stateOrProvince,postalCode,country,
                    county,geoCode,schoolDistrictCode,source) 
            VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
            ''',
            (
            field1,     #id
            field2,     #name
            field3,     #billingAddress
            field4,     #frontOfficeId
            field5,     #link
            field6,     #isArchived
            field7,     #representativeUsers
            field8,     #statusId
            field9,     #status
            field10,    #industry
            field11,    #createdDate
            field12,    #lastUpdatedDate
            field13,    #lastActivityDate
            field14,    #latestActivityName
            field15,    #openJobs
            field16,    #street1
            field17,    #street2
            field18,    #city
            field19,    #stateOrProvince
            field20,    #postalCode
            field21,    #country
            field22,    #county
            field23,    #geoCode
            field24,    #schoolDistrictCode
            'PL1'     #source
            )
        )

    cursor.commit()

我正在将我的数据从 json 转换为数据帧,然后从那里将其输入到 sql。

这是我的数据框输入的示例:

            field1  = df_t['id'].values[0]
            field2  = df_t['name'].values[0]
            field3  = df_t['billingAddress'].values[0]
            field4  = df_t['frontOfficeId'].values[0]
            field5  = df_t['link'].values[0]
            field6  = df_t['isArchived'].values[0]
            field7  = df_t['representativeUsers'].values[0]
            field8  = df_t['statusId'].values[0]
            field9  = df_t['status'].values[0]
            field10 = df_t['industry'].values[0]
            field11 = df_t['createdDate'].values[0]
            field12 = df_t['lastUpdatedDate'].values[0]
            field13 = df_t['latestActivityDate'].values[0]
            field14 = df_t['latestActivityName'].values[0]
            field15 = df_t['openJobs'].values[0]
            field16 = df_t['street1'].values[0]
            field17 = df_t['street2'].values[0]
            field18 = df_t['city'].values[0]
            field19 = df_t['state_Province'].values[0]
            field20 = df_t['postalCode'].values[0]
            field21 = df_t['country'].values[0]
            field22 = df_t['county'].values[0]
            field23 = df_t['geoCode'].values[0]
            field24 = df_t['schoolDistrictCode'].values[0]

Field25 是一个常量,这就是它出现在我的 sql 语句中的原因。

我的样本 json:

{'id': 4177713, 'name': '', 'mainAddress': {'street1': None, 'street2': None, 'city': None, 'state_Province': 'AR', 'postalCode': None, 'country': 'US', 'county': None, 'geoCode': None, 'schoolDistrictCode': None}, 'billingAddress': None, 'frontOfficeId': 11259, 'link': 'https://apisite.comPyodbc Error "A TVP's rows must be Sequence objects.", 'HY000'/app/#/company/4177713', 'isArchived': True, 'representativeUsers': [151209], 'statusId': 6492, 'status': 'Delete', 'industry': None, 'createdDate': '2008-04-15T15:28:00', 'lastUpdatedDate': None, 'latestActivityDate': None, 'latestActivityName': None, 'openJobs': 0}

因为df_t['representativeUsers'].values[0]有一个带'[]'的字符串,添加 df_t['representativeUsers'].values[0][0] 解决了我的问题。