python用外键更新mysql表

时间:2018-10-15 19:10:05

标签: python python-3.x mysql-python

我正在尝试调用mysql存储过程。我需要从csv文件以及从数据库查询的字典列中添加参数。在我的数据库中,server_id是保护点表的外键。当我尝试将serverid添加为参数时,出现以下断言错误。我不确定py文件是否可以告诉我在serverdict函数中定义了serverid。

#create dictionary with server_id foreign key
import mysql.connector
# create database connection

conn = mysql.connector.connect(host='xx', user='xx', password='xx', db='xx')


# cursor = conn.cursor()

def serverdict():
    cursor = conn.cursor(dictionary=True)

    cursor.execute("select server_id, name from servers")

    for serverid in cursor:
        print("* {server_id}: {name}".format(**serverid))

import mysql.connector
import csv
from serverdict import serverdict
# open csv file


with open('DBstatus.csv') as dbcsvfile:
    dbstatus = csv.DictReader(dbcsvfile)
    for row in dbstatus:
        print(row['Report Id'], row['Status Id'], row['Host Name'], row["OS Type"], row['Host Description'], row['License Type'],
              row['One-Way Enabled'], row['FS Agent Registration Status'], row['FS Agent Version'], row['Host Name'], row['Last Policy Update'],
              row['Guard Point Id'], row['Guard Path'], row['Policy Id'], row['Policy Name'], row['Guard Enabled'],
              row['Guard Point Status'], row['Error Reason'])

        serverdict()

        #create database connection

        conn = mysql.connector.connect(host='xxxx', user='xxx', password='xxx', db='xxx')

        cursor = conn.cursor()


        #Create parameters for stored procedure
        guardpointparams = (row['FS Agent Registration Status'], row['FS Agent Version'], row['Guard Path'], serverid['server_id'])

        #call stored procedure
        updateguardpointstable = cursor.callproc('update_guardpoints', guardpointparams)
        conn.commit()
        conn.close()

Traceback (most recent call last):

更新Guardpoints Table.py”,第29行,在     guardpointparams =(行['FS代理注册状态'],行['FS代理版本'],行['保护路径'],服务器ID ['server_id']) NameError:名称“ serverid”未定义

0 个答案:

没有答案