NameError: name is not defined function

NameError: name is not defined function

我正在尝试在 Python 中编写一个函数,该函数向数据库发送 SQL 查询,将检索到的数据存储在 Python 数据框中,并输出到 Excel 在命名选项卡上用于多个查询。当 "shortcut" 的名称以前是 运行 手动但我无法在函数调用的每个实例中编写新的 "shortcut" 名称时,我得到它的工作。

有人可以帮我调试这个问题吗?

这是 Python 代码:

import cx_Oracle
import pandas as pd
import xlwt as xls_write

writer = pd.ExcelWriter('<Excel File>')

def executeSQL(shortcut, tab, SQL):
    conn = cx_Oracle.connect('<Connection String')
    cursor= conn.cursor()
    cursor.execute (SQL)
    shortcut = pd.DataFrame(cursor.fetchall())
    shortcut.columns = [rec[0] for rec in cursor.description]
    cursor.close()
    conn.close()
    shortcut.to_excel(writer, tab, index=False)

executeSQL(tab1, 'Tab 1', "<SQL Statement>")
executeSQL(tab2, 'Tab 2', "<SQL Statement>")
executeSQL(tab3, 'Tab 3', "<SQL Statement>")
executeSQL(tab4, 'Tab 4', "<SQL Statement>")
executeSQL(tab5, 'Tab 5', "<SQL Statement>")

为什么要传递快捷方式参数,然后在函数中重新定义它?我通常使用 pyodbc 进行连接,因为我没有您的实际数据,下面是我将如何执行此操作的模型:

import cx_Oracle
import pandas as pd
import xlwt as xls_write

writer = pd.ExcelWriter('<Excel File>')
conn = cx_Oracle.connect('<Connection String')

def executeSQL(tab, SQL, connection):

    df = pd.read_sql(SQL,connection)
    df.to_excel(writer, tab, index=False)

executeSQL('Tab 1', "<SQL Statement>")
executeSQL('Tab 2', "<SQL Statement>")
executeSQL('Tab 3', "<SQL Statement>")
executeSQL('Tab 4', "<SQL Statement>")
executeSQL('Tab 5', "<SQL Statement>")

conn.close()