56 lines
1.5 KiB
Python
56 lines
1.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
import sqlite3
|
|
import sys
|
|
|
|
# 设置输出编码为UTF-8
|
|
sys.stdout.reconfigure(encoding='utf-8')
|
|
|
|
db_path = 'instance/database.db'
|
|
|
|
try:
|
|
conn = sqlite3.connect(db_path)
|
|
cursor = conn.cursor()
|
|
|
|
# 检查数据库编码
|
|
cursor.execute('PRAGMA encoding')
|
|
encoding = cursor.fetchone()[0]
|
|
print(f"数据库编码: {encoding}")
|
|
print("-" * 50)
|
|
|
|
# 获取所有表
|
|
cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
|
|
tables = [row[0] for row in cursor.fetchall()]
|
|
print(f"\n数据表列表 (共{len(tables)}个):")
|
|
for table in tables:
|
|
print(f" - {table}")
|
|
|
|
print("\n" + "=" * 50)
|
|
|
|
# 检查每个表的数据
|
|
for table in tables:
|
|
cursor.execute(f"SELECT COUNT(*) FROM {table}")
|
|
count = cursor.fetchone()[0]
|
|
print(f"\n表 '{table}': {count} 条记录")
|
|
|
|
# 显示表结构
|
|
cursor.execute(f"PRAGMA table_info({table})")
|
|
columns = cursor.fetchall()
|
|
print(f" 字段: {', '.join([col[1] for col in columns])}")
|
|
|
|
# 显示前3条数据示例
|
|
if count > 0:
|
|
cursor.execute(f"SELECT * FROM {table} LIMIT 3")
|
|
rows = cursor.fetchall()
|
|
print(f" 示例数据 (前{len(rows)}条):")
|
|
for i, row in enumerate(rows, 1):
|
|
print(f" 记录{i}: {row[:5]}...") # 只显示前5个字段
|
|
|
|
conn.close()
|
|
print("\n" + "=" * 50)
|
|
print("数据库检查完成!")
|
|
|
|
except Exception as e:
|
|
print(f"错误: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|