想做一个简单的数据库表的统计工具。 用 AI,感觉整个人脑子都不好用了。 import pyodbc import pandas as pd import matplotlib.pyplot as plt import seaborn as sns from datetime import datetime import warnings warnings.filterwarnings('ignore') class MSSQLTableAnalyzer: def __init__(self, server, database, username=None, password=None, trusted_connection=True): """ Initialize MSSQL connection Args: server: SQL Server name or IP database: Database name username: Username (optional if using Windows Authentication) password: Password (optional if using Windows Authentication) trusted_connection: Use Windows Authentication (default: True) """ self.server = server self.database = database self.username = username self.password = password self.trusted_connection = trusted_connection self.connection = None def connect(self): """Establish connection to MSSQL database""" try: if self.trusted_connection: # Windows Authentication connection_string = f""" DRIVER={{ODBC Driver 17 for SQL Server}}; SERVER={self.server}; DATABASE={self.database}; Trusted_Connection=yes; """ else: # SQL Server Authentication connection_string = f""" DRIVER={{ODBC Driver 17 for SQL Server}}; SERVER={self.server}; DATABASE={self.database}; UID={self.username}; PWD={self.password}; """ self.connection = pyodbc.connect(connection_string) print(f"✅ Successfully connected to {self.database} on {self.server}") return True except Exception as e: print(f"❌ Connection failed: {str(e)}") return False def get_table_counts(self): """Query all tables and their row counts""" if not self.connection: print("❌ No database connection established") return None try: # Query to get all user tables and their row counts query = """ SELECT t.TABLE_SCHEMA as [Schema], t.TABLE_NAME as [Table_Name], p.rows as [Row_Count] FROM INFORMATION_SCHEMA.TABLES t INNER JOIN sys.tables st ON st.name = t.TABLE_NAME INNER JOIN sys.partitions p ON st.object_id = p.object_id WHERE t.TABLE_TYPE = 'BASE TABLE' AND p.index_id < 2 ORDER BY p.rows DESC, t.TABLE_SCHEMA, t.TABLE_NAME """ df = pd.read_sql_query(query, self.connection) print(f"📊 Found {len(df)} tables in database") return df except Exception as e: print(f"❌ Query failed: {str(e)}") return None def create_bar_chart(self, df, max_tables=20, chart_type='horizontal'): """…