如何在mysql_insert_titles函数中使用游标?没有重新连接“再次”?

时间:2019-07-05 23:13:07

标签: mysql sql connect

我正在尝试在mysql_insert_titles函数中使用光标对象?但是,总是会出现错误,即没有定义光标的名称错误。

我尝试在mysql_insert_titles函数中“重新连接”它,并将它也连接到my_sql connect函数之外,以便我可以在任何函数中使用变量,但是另一个错误是未定义args。

def mysql_connect(args):
    try:
        username = args[0]
        password = args[1]
        database = args[2]
        global connection
        connection = pymysql.connect(host= 'localhost', user=username, password=password, database=database, charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
        cursor=connection.cursor()
        print('\n'+username + ' is now connected to the ' + database + ' database.' + '\n')

    except Exception as e:
        print("Couldn't log " + username + " in to MySQL server.")
    return None
def mysql_insert_titles(agglevel_titles,fips_titles,industry_titles,ownership_titles):

    rows_added=0
    agglevel_titles_sql= "INSERT INTO 'agglevel_titles' ('agglvl_code','agglvl_title') VALUES (%s,%s)"
    for i in agglevel_titles: 
        code = i['agglvl_code']
        title = i['agglvl_title']
        rows_added += cursor.execute(agglevel_titles_sql, (code, title))

    connection.commit()

最后,我需要计算插入的行数。但是我需要先解决我的错误。

0 个答案:

没有答案