import psycopg2 import json try: conn = psycopg2.connect( host='localhost', port=5432, database='inventory_system', user='test', password='1234' ) cur = conn.cursor() cur.execute('SELECT id, action, target_name, details FROM audit_logs ORDER BY id DESC LIMIT 3') rows = cur.fetchall() print('=== 最新3条审计日志 ===') for row in rows: print(f'ID: {row[0]}') print(f'Action: {row[1]}') print(f'Target: {row[2]}') details = row[3] if details: # 格式化显示 if isinstance(details, str): try: details = json.loads(details) except: pass print(f'Details: {json.dumps(details, indent=2, ensure_ascii=False)}') else: print(f'Details: None') print('---') cur.close() conn.close() except Exception as e: print(f'Error: {e}')