python3循环通过一系列DB,然后连接

发布于 2025-01-27 20:52:32 字数 619 浏览 3 评论 0原文

我正在尝试循环浏览许多具有相同连接信息的MySQL主机,并在它们中执行相同的查询&获取结果。

我仍在学习Python&陷入以下内容;

import pymysql

ENDPOINTS=['endpoint01', 'endpoint02', 'endpoint03', 'endpoint04']
USER="SOME_USER"
PASS="SOME_PASSWORD"

print("Testing")

for x in ENDPOINTS:
  # Open database connection
  DATAB = pymysql.connect(x,USER,PASS)
  cursor = DATAB.cursor()
  cursor.execute("show databases like 'THIS%'")
  data = cursor.fetchall()
  print (data)
  DATAB.close()

这是我收到的错误;

DATAB = pymysql.connect(x,USER,PASS)
TypeError: __init__() takes 1 positional argument but 4 were given

I am trying to loop through a number of mysql hosts which have the same connection info and execute the same query on each of them & fetch the results.

I'm still learning python & am stuck on the following;

import pymysql

ENDPOINTS=['endpoint01', 'endpoint02', 'endpoint03', 'endpoint04']
USER="SOME_USER"
PASS="SOME_PASSWORD"

print("Testing")

for x in ENDPOINTS:
  # Open database connection
  DATAB = pymysql.connect(x,USER,PASS)
  cursor = DATAB.cursor()
  cursor.execute("show databases like 'THIS%'")
  data = cursor.fetchall()
  print (data)
  DATAB.close()

And this is the error I receive;

DATAB = pymysql.connect(x,USER,PASS)
TypeError: __init__() takes 1 positional argument but 4 were given

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮年 2025-02-03 20:52:32

您正在错误地传递参数。尝试
datab = pymysql.connect(host = x,user = user = user = uses,password = pass)

You're passing the parameters incorrectly. Try
DATAB = pymysql.connect(host=x,user=USER,password=PASS):

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文