我正在尝试使用teradatasql模块通过Python执行一些基本的SQL。代码似乎正在运行并执行SQL :但是,Python本身的执行在下面重现的代码末尾出现错误而结束。目前,我需要使用pandas在SQL的输出上运行额外的数据预处理步骤,但是更大的程序将不会在操作错误之后继续执行(甚至不会通过除teradatsql.OperationalError之外的try/except块)。因此,即使SQL在这个问题上执行得很好,我也需要解决它。
有什么建议吗?谢谢!
错误:
teradatasql.OperationalError: 1不是有效的连接池句柄
代码:
import teradatasql
import os
def refresh_table():
usr = ****1
with open(f'C:\\Users\\{usr}\\Documents\\my_td_password.txt', 'r') as my_pwd_f:
pw = my_pwd_f.read()
with teradatasql.connect(host = '*******2'
, user = usr
, password = pw
, ) as con:
with con.cursor() as cur:
with open('C:\\Users\\****1\\Documents\\test.sql', 'r') as my_sql:
sql_script = my_sql.read()
for sql_block in sql_script.split(';'):
try:
cur.execute(sql_block)
print("Block executed")
except ValueError:
print("Failure to execute block: ValueError")
finally:
print(sql_block)
my_sql.close()
print("SQL file closed")
con.close()
print("Connection closed")
refresh_table()发布于 2021-09-09 20:10:13
通过从末尾删除con.close()修复-正如Fred所指出的,with块在完成执行时隐式地关闭连接
https://stackoverflow.com/questions/69120862
复制相似问题