# complete_diagnosis.py
import sqlite3
import os
import glob
import sys
def find_database_file():
"""
查找数据库文件的完整路径
"""
print("🔍 正在查找数据库文件...")
# 可能的数据库文件扩展名
extensions = ['', '.db', '.sqlite', '.sqlite3', '.db3']
# 可能的文件路径(当前目录和常见位置)
possible_paths = [
'.', # 当前目录
'..', # 上级目录
os.path.expanduser('~'), # 用户主目录
os.path.expanduser('~/Desktop'),
os.path.expanduser('~/Documents'),
]
found_files = []
# 检查所有可能的路径和扩展名组合
for path in possible_paths:
for ext in extensions:
pattern = os.path.join(path, f"taobao_data{ext}")
# 使用glob处理通配符和波浪号扩展
expanded_pattern = os.path.expanduser(pattern)
matches = glob.glob(expanded_pattern)
for match in matches:
if os.path.isfile(match):
abs_path = os.path.abspath(match)
found_files.append(abs_path)
print(f"✅ 找到数据库文件: {abs_path}")
return found_files
def get_database_info(db_path):
"""
获取数据库的详细信息
"""
print(f"\n📊 数据库信息: {db_path}")
print("=" * 50)
try:
# 连接到数据库
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# 获取所有表
cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
tables = [table[0] for table in cursor.fetchall()]
print(f"📋 数据库中的表 ({len(tables)}):")
for table in tables:
print(f" - {table}")
# 检查taobao_products表是否存在
if 'taobao_products' in tables:
print(f"\n✅ 表 'taobao_products' 存在")
# 获取表结构
cursor.execute("PRAGMA table_info(taobao_products)")
columns = cursor.fetchall()
print(f"📋 表结构 (共 {len(columns)} 列):")
for col in columns:
print(f" - {col[1]} ({col[2]})")
# 获取记录数量
cursor.execute("SELECT COUNT(*) FROM taobao_products")
count = cursor.fetchone()[0]
print(f"📈 表中的记录数: {count}")
else:
print(f"\n❌ 表 'taobao_products' 不存在")
# 查找类似的表名
similar_tables = [t for t in tables if 'taobao' in t.lower() or 'product' in t.lower()]
if similar_tables:
print("🤔 找到以下可能相关的表:")
for table in similar_tables:
print(f" - {table}")
conn.close()
except Exception as e:
print(f"❌ 无法打开数据库: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
# 查找数据库文件
db_files = find_database_file()
if not db_files:
print("❌ 未找到数据库文件")
# 尝试查找其他可能的名称
other_names = ['taobao', 'products', 'data', 'spider_data']
for name in other_names:
print(f"\n尝试查找名称包含 '{name}' 的数据库文件...")
db_files = find_database_file(name)
if db_files:
break
# 显示每个找到的数据库的详细信息
for db_file in db_files:
get_database_info(db_file)
print("\n" + "=" * 50 + "\n")


明明数据库里有表,程序检查出来却说没有