Python pyodbc cursor.execute(查询)不处理.exe编译

2024-05-12 22:39:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Python创建一个警报,该警报连接到SQL数据库,执行SELECT,并基于结果通过SMTP发送警报。我把它都记下来了,它在从VisualStudio执行Python脚本时起作用。但是,如果我使用pyinstaller将其编译为.exe(我需要在未安装Python的服务器上从Windows任务调度器执行),并尝试运行它,它将在执行查询的行上停止。代码如下:

for BM in servers: 
        try:
            server = BM[0]
            db = BM[1]

            conn = pyodbc.connect('driver={%s};server=%s;database=%s;uid=%s;pwd=%s' % ( driver, server, db, user, password ) )
            log.write("Conectado" + '\n')

            cursor = conn.cursor()
            log.write("Cursor creado" + '\n')

            try:
                cursor.execute(query)
                log.write("Query ejecutada" + '\n')

            except pyodbc.Error as ex:
                log.write(ex + '\n')

        except pyodbc.Error as ex:
            log.write(ex + '\n')

停止脚本的行是cursor.execute(查询)。而“查询”是一个简单的选择*表,没有条件

我怎样才能做到这一点


Tags: 脚本logexecutedbserverdriver警报conn