pyodbc调用存储过程的参数名称;数据类型问题

问题描述 投票:0回答:1

当运行cursor_insert.execute(sql,params)时,我不断收到“[SQL ERROR]将数据类型nvarchar转换为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()

python pyodbc
1个回答
1
投票

你没有传递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()
© www.soinside.com 2019 - 2024. All rights reserved.