gpt4 book ai didi

python - 如何同时使用2个数组?

转载 作者:行者123 更新时间:2023-11-30 22:21:59 26 4
gpt4 key购买 nike

抱歉,我不确定我使用的术语是否正确。我有一个程序可以循环访问服务器名称的“myConnect”数组。它创建到每个服务器名称的连接并运行查询。这很好用。

但是如果我在每台服务器上也有多个数据库名称怎么办?循环浏览数据库名称和服务器名称列表的最佳方法是什么?这是我的仅用于服务器名称的工作代码:

import os
import pypyodbc

def func1(connections, firstname, lastname, uname):

try:
connection = pypyodbc.connect('Driver={SQL Server};Server=' + connections + ';Database=Database1;Trusted_Connection=yes;')
except pypyodbc.Error as ex:
sqlstate = ex.args[0]
if sqlstate == '28000':
print("You do not have access.")
cursor = connection.cursor()
SQLCommand = ("SELECT Active, UserName, FirstName, LastName "
"FROM dbo.My_Table " # table name
"(nolock)"
"WHERE FirstName LIKE ? AND LastName LIKE ? AND UserName LIKE ?")
Values = [firstname + '%', lastname + '%', '%' + uname + '%']
cursor.execute(SQLCommand,Values)
results = cursor.fetchone()
if results:
print(connections + " " + str(results[0]) + " " + str(results[1]) + " " + str(results[2]) + " " + str(results[3])) # enters results in entry
connection.close()
else:
print(connections + " - NO")
connection.close()

def start():

os.system('cls' if os.name == 'nt' else 'clear')
print (" ")
while True:
print("\nPress Ctrl + c to exit.")
firstname = input('Type the first name: ')
lastname = input('Type the last name: ')
myConnect = ['server1', 'server2', 'server3']
for connections in myConnect:
func1(connections, firstname, lastname, uname)

start()

我应该在 myConnect 下创建另一个数据库阵列吗?有没有办法将它们组合在一起?也许是这样的?

import os
import pypyodbc

def func1(connections, names, firstname, lastname, uname):

try:
connection = pypyodbc.connect('Driver={SQL Server};Server=' + connections + ';Database=' + names + ';Trusted_Connection=yes;')
except pypyodbc.Error as ex:
sqlstate = ex.args[0]
if sqlstate == '28000':
print("You do not have access.")
cursor = connection.cursor()
SQLCommand = ("SELECT Active, UserName, FirstName, LastName "
"FROM dbo.My_Table " # table name
"(nolock)"
"WHERE FirstName LIKE ? AND LastName LIKE ? AND UserName LIKE ?")
Values = [firstname + '%', lastname + '%', '%' + uname + '%']
cursor.execute(SQLCommand,Values)
results = cursor.fetchone()
if results:
print(connections + " " + str(results[0]) + " " + str(results[1]) + " " + str(results[2]) + " " + str(results[3])) # enters results in entry
connection.close()
else:
print(connections + " - NO")
connection.close()

def start():

os.system('cls' if os.name == 'nt' else 'clear')
print (" ")
while True:
print("\nPress Ctrl + c to exit.")
firstname = input('Type the first name: ')
lastname = input('Type the last name: ')
myConnect = ['server1', 'server1', 'server2', 'server2', 'server3']
databases = ['Database1', 'Database2', 'Database3', 'Database4', 'Database5']
for connections in myConnect:
for names in databases:
func1(connections, names, firstname, lastname, uname)

start()

最佳答案

serverDB = {"server1": ['Database1', 'Database2'], 
"server2": ['Database3', 'Database4'],
"Server3": ['Database5']}

for server in serverDB:
print("connecting to server: " + server)
for db in serverDB[server]:
print ( "querying db: " + db)

输出:

connecting to server: server1
querying db: Database1
querying db: Database2
connecting to server: server2
querying db: Database3
querying db: Database4
connecting to server: Server3
querying db: Database5

关于python - 如何同时使用2个数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48468316/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com