我想使用下面将要看到的python脚本创建到SQL Server数据库的连接。实际上,我设法连接到一个数据库,但是我想在数据库列表上循环,以便每次指向数据库时都可以直接连接。 我的问题是如何使数据库名称成为变量。请问你能帮帮我吗?!预先谢谢你
from os import listdir
from os.path import isfile, join
import pyodbc
import pandas as pd
mypath = 'C:\\Users\\DataBaseList'
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
for i in range (len(onlyfiles)) :
print (onlyfiles[i])
connection = pyodbc.connect("Driver={ODBC Driver 11 for SQL Server};"
"Server=FR0010APP31;"
"Database= Base;"
"uid=*****;pwd=*****")
cursor = connection.cursor()
sql='Select * from Activity'
df= pd.read_sql(sql, connection)
df.to_csv('DatabaseFile')