function1没有返回conn变量

时间:2015-12-22 18:23:57

标签: python-3.x

def function1():
    conn = sqlite3.connect('mydb.db')
    connection = conn.cursor()

    return conn, connection

def function2(conn, connection):
    conn.execute("""
     INSERT INTO category
     (cat_name)
     VALUES (?) """, (cat,)
     )       
     conn.commit()

def main():
    connection = function1()
    categories = function2(conn, connection)

它没说:

categories = function2(conn, connection)
NameError: name 'conn' is not defined

知道为什么吗?谢谢

1 个答案:

答案 0 :(得分:0)

您从未在main()中定义conn。可能你想在connection中使用元组的值。

E.g。

conn, connection = function1()
categories = function2(conn, connection)