1.源代码
#-----------获取数据库视图列表-----------------------------
# -------密码含特殊字符使用
from urllib.parse import quote_plus as urlquotefrom sqlalchemy import create_engine, MetaData, inspect# 替换为你的数据库连接字符串
DRIVER = "ODBC Driver 18 for SQL Server"
USERNAME = "sa"
PSSWD = 'aaaaa@!@#$%$%^^&bbbb12312' #----注意里面有特殊字符
SERVERNAME = "172.168.1.1"
PORT=1433
INSTANCENAME = "\SQLEXPRESS"
DB = "TESTDATA"engine = create_engine(f"mssql+pyodbc://{USERNAME}:{urlquote(PSSWD)}@{SERVERNAME}{INSTANCENAME}:{PORT}/{DB}?driver={DRIVER}&TrustServerCertificate=yes", fast_executemany=True)inspector = inspect(engine)# 获取所有视图的名称
views = inspector.get_view_names()print("视图列表:")
for view in views:print(view)
重点说明:
1)连接数据库的密码中存在特殊字符怎么处理:
使用urllib.parse的quote_plus
2)获取视图名称列表:
使用sqlalchemy inspect.get_view_names
如果对你有用,请点个赞吧!