为什么当 pyodbc 游标在 main() 中时 "name 'cursor' is not defined"?
Why "name 'cursor' is not defined" when pyodbc cursor is in main()?
我是 运行 一个 Python 程序,浓缩在下面,来自命令行,它给了我一个错误 (name 'cursor' is not defined
) 并指向 cursor.fetchmany()
靠近底部的行。但是,如果我将 main()
的前两行、conn
和 cursor
赋值移动到 main()
的上方和外部,程序就会按预期运行.当我在 main()
中分配 cursor
时,为什么这不起作用?我很想知道这是否只是 pyodbc 库的一个怪癖,或者有更好的方法让它在 main()
内部工作,或者实际上可以将分配留在程序主体中.
import pyodbc
import csv
# ...other modules...
def main():
conn = pyodbc.connect(DSN=abcxyz, autocommit=True)
cursor = conn.cursor()
# ...sys.argv assignments and validation...
pull_data(query_file, start_date, end_date, out_file)
def pull_data(query_file, start_date, end_date, out_file):
# ...prepare query as string, date range as list...
for date in list_dates:
cursor.execute(query, date)
append_rows(out_file)
def append_rows(out_file):
with open(out_file, 'a', newline='') as file:
writer = csv.writer(file)
while True:
results = cursor.fetchmany(chunk_size)
if not results:
break
for result in results:
writer.writerow(result)
if __name__ == '__main__':
main()
因为 append_rows()
或 pull_data()
中未定义游标。您在 main()
中定义它,因此它只能在 main()
.
中访问
解决此问题的最佳方法可能是将光标对象传递给 append_rows()
和 pull_data()
。
您收到 cursor
未定义错误的原因是因为它不存在于您的函数中。变量只存在于它们定义的范围内。
当您在 main
之外定义 cursor
时,它被声明为全局变量,这意味着它可以在脚本的所有范围内访问。
尝试:
import pyodbc
import csv
# ...other modules...
def main():
# Since conn and cursor are being declared here, they only exist within the scope of this function, not other functions that are called.
conn = pyodbc.connect(DSN=abcxyz, autocommit=True)
cursor = conn.cursor()
# ...sys.argv assignments and validation...
# make sure we give the cursor to pull_data so it will exist in its scope!
pull_data(query_file, start_date, end_date, out_file, cursor)
def pull_data(query_file, start_date, end_date, out_file, cursor):
# ...prepare query as string, date range as list...
for date in list_dates:
cursor.execute(query, date)
# Pass the cursor to append_rows
append_rows(out_file, cursor)
# cursor is actually passed to the append_rows function. Now it exists in this scope
def append_rows(out_file, cursor):
with open(out_file, 'a', newline='') as file:
writer = csv.writer(file)
while True:
results = cursor.fetchall()
for result in results:
writer.writerow(result)
if __name__ == '__main__':
main()
我还建议您阅读 。
我是 运行 一个 Python 程序,浓缩在下面,来自命令行,它给了我一个错误 (name 'cursor' is not defined
) 并指向 cursor.fetchmany()
靠近底部的行。但是,如果我将 main()
的前两行、conn
和 cursor
赋值移动到 main()
的上方和外部,程序就会按预期运行.当我在 main()
中分配 cursor
时,为什么这不起作用?我很想知道这是否只是 pyodbc 库的一个怪癖,或者有更好的方法让它在 main()
内部工作,或者实际上可以将分配留在程序主体中.
import pyodbc
import csv
# ...other modules...
def main():
conn = pyodbc.connect(DSN=abcxyz, autocommit=True)
cursor = conn.cursor()
# ...sys.argv assignments and validation...
pull_data(query_file, start_date, end_date, out_file)
def pull_data(query_file, start_date, end_date, out_file):
# ...prepare query as string, date range as list...
for date in list_dates:
cursor.execute(query, date)
append_rows(out_file)
def append_rows(out_file):
with open(out_file, 'a', newline='') as file:
writer = csv.writer(file)
while True:
results = cursor.fetchmany(chunk_size)
if not results:
break
for result in results:
writer.writerow(result)
if __name__ == '__main__':
main()
因为 append_rows()
或 pull_data()
中未定义游标。您在 main()
中定义它,因此它只能在 main()
.
解决此问题的最佳方法可能是将光标对象传递给 append_rows()
和 pull_data()
。
您收到 cursor
未定义错误的原因是因为它不存在于您的函数中。变量只存在于它们定义的范围内。
当您在 main
之外定义 cursor
时,它被声明为全局变量,这意味着它可以在脚本的所有范围内访问。
尝试:
import pyodbc
import csv
# ...other modules...
def main():
# Since conn and cursor are being declared here, they only exist within the scope of this function, not other functions that are called.
conn = pyodbc.connect(DSN=abcxyz, autocommit=True)
cursor = conn.cursor()
# ...sys.argv assignments and validation...
# make sure we give the cursor to pull_data so it will exist in its scope!
pull_data(query_file, start_date, end_date, out_file, cursor)
def pull_data(query_file, start_date, end_date, out_file, cursor):
# ...prepare query as string, date range as list...
for date in list_dates:
cursor.execute(query, date)
# Pass the cursor to append_rows
append_rows(out_file, cursor)
# cursor is actually passed to the append_rows function. Now it exists in this scope
def append_rows(out_file, cursor):
with open(out_file, 'a', newline='') as file:
writer = csv.writer(file)
while True:
results = cursor.fetchall()
for result in results:
writer.writerow(result)
if __name__ == '__main__':
main()
我还建议您阅读 。