pyodbc调用参数名为的存储过程;数据类型为issu

2024-10-04 11:25:59 发布

您现在位置:Python中文网/ 问答频道 /正文

运行光标时_插入.执行(sql,params),我不断得到“[sql ERROR]ERROR converting data type nvarchar to float”,即使我的源和目标db表将数据与存储过程一起定义为float、nvarchar和nvarchar。你知道吗

是否将我的参数设置为一个名为“params”的新变量导致了数据类型的更改?如果是的话,我该怎么解决呢?(在阅读一些Python文档时,它不应该更改数据类型,对吗?)你知道吗

# Create cursor associated with connection
cursor=conn.cursor()
cursor_select = conn.cursor()
cursor_insert = conn.cursor()

if conn:
    print('***** Connected to DCPWDBS289 *****')

select_str="SELECT TOP 5 Incident_ID,Incident_Type,Priority FROM 
incidents_all WHERE incidents_all.Status NOT IN ('Closed','Resolved')"

cursor_select.execute(select_str)

while True:

    row = cursor_select.fetchone()
    if not row:
        break
    print(' Row:     ', row)

    IncIncident_ID      = row[0]    # Float
    IncIncident_Type    = row[1]    # Str
    IncPriority         = row[2]    # Str

    sql = """EXEC ITSM.dbo.ITSM_LOAD @IncIncident_ID=?, 
    @IncIncident_Type=?,@IncPriority=?"""

    params = ('IncIncident_ID','IncIncident_Type','IncPriority')

    cursor_insert.execute(sql,params)

del cursor_insert
cursor.commit()
conn.close()


Tags: toidsqltypeerrorparamsfloatconn
1条回答
网友
1楼 · 发布于 2024-10-04 11:25:59

您不是在传递parameter值,而是在传递字符串文本,请尝试以下操作:

# Create cursor associated with connection
cursor=conn.cursor()
cursor_select = conn.cursor()
cursor_insert = conn.cursor()

if conn:
    print('***** Connected to DCPWDBS289 *****')

select_str="SELECT TOP 5 Incident_ID,Incident_Type,Priority FROM 
incidents_all WHERE incidents_all.Status NOT IN ('Closed','Resolved')"

cursor_select.execute(select_str)

while True:

    row = cursor_select.fetchone()
    if not row:
        break
    print(' Row:     ', row)

    IncIncident_ID      = row[0]    # Float
    IncIncident_Type    = row[1]    # Str
    IncPriority         = row[2]    # Str

    sql = """EXEC ITSM.dbo.ITSM_LOAD @IncIncident_ID=?, 
    @IncIncident_Type=?,@IncPriority=?"""

    params = (IncIncident_ID, IncIncident_Type, IncPriority)

    cursor_insert.execute(sql,params)

del cursor_insert
cursor.commit()
conn.close()

相关问题 更多 >