简体   繁体   English

如何调用字典作为存储过程的参数

[英]How to call a dictionary as a parameter for stored procedure

I am trying to use the dictionary in a parameter to call a stored procedure to update a table that uses a value in the Dict as a foreign key. 我正在尝试在参数中使用字典来调用存储过程来更新将Dict中的值用作外键的表。

Dict data (ID is the foreign key) ex: server1, ID1 server5, ID2 Dict数据(ID是外键)例如:server1,ID1 server5,ID2

ID is the foreign key on the table i am trying to update and needs to be the parameter when calling the stored procedure. ID是我尝试更新的表上的外键,并且在调用存储过程时需要作为参数。 It appears that when i pass in the dictionary as a parameter it is pulling server1 instead of ID1 and causing a key error. 看来,当我将字典作为参数传递时,它将拉动server1而不是ID1并导致键错误。

with open('DBstatus.csv') as dbcsvfile:
    dbstatus = csv.DictReader(dbcsvfile)
    for row in dbstatus:

#Create parameters for stored procedure
        guardpointparams = (row['FS Agent Registration Status'], row['FS Agent Version'], row['Guard Path'], serverdict[row['Host Name']], policydict[row['Policy Name']], 0)
        #call stored procedure
        updateguardpointstable = cursor.callproc('update_guardpoints', guardpointparams)

#i created the dictionary from a different script while updating the 
"servers" table where the DB is creating the returnvalue (ID) from the 
stored procedure see sample below:

serverdict[row['Host Name']] = returnvalue[3]

Traceback: guardpointparams = (row['FS Agent Registration Status'], row['FS Agent Version'], row['Guard Path'], serverdict[row['Host Name']], policydict[row['Policy Name']], 0) KeyError: 'server1' 追溯:guardpointparams =(row ['FS代理注册状态'],第['FS代理版本'],第['Guard Path'],serverdict [row ['Host Name']],policydict [row ['Policy Name ']],0)KeyError:“ server1”

row['Host Name'] appears to be server1 . row['Host Name']似乎是server1 The dictionary serverdict does not have a key named server1 字典serverdict没有名为server1的键。

It looks like either your serverdict or your policydict is not defined. 看来您的serverdictpolicydict没有定义。 At least you don't provide the definition in your example. 至少您没有在示例中提供定义。 Further, the traceback tells you that one of these don't have the key you are looking for. 此外,回溯告诉您其中之一没有您要查找的密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM