m0_37908418
2021-07-06 16:39
采纳率: 60%
浏览 87

python 连接多个sql sql并执行sql 怎样用for或其它循环操作

我想连接20个sqlserver,下面是单独连接并运行,怎样一次执行连接20个sql 并在上面执行写好的sql,连接异常时要报错.谢谢

sqlserver='xx.xx.xxx.32',sqlserver1='xx.xx.xxx.33',sqlserver2='xx.xx.xxx.34'
try:
conn = pymssql.connect(sqlserver, user, password, database)
sql_path = 'C:\count.sql'
sql = open(sql_path,'r',encoding ="utf-8-sig")
sqltxt = sql.readlines()
sql.close()
sql = "".join(sqltxt)
df = pd.read_sql(sql,conn)
except e
try:
conn1 = pymssql.connect(sqlserver1, user, password, database)
sql_path = 'C:\count.sql'
sql = open(sql_path,'r',encoding ="utf-8-sig")
sqltxt = sql.readlines()
sql.close()
sql = "".join(sqltxt)
df = pd.read_sql(sql,conn1)
except e

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题