#!/usr/bin/env python3
"""
检查sqlite数据库中有多少bar数据 - vnpy_sqlite表名是dbbardata
"""

import sqlite3
from vnpy.trader.constant import Exchange

db_path = '/root/.vntrader/database.db'

conn = sqlite3.connect(db_path)
cursor = conn.cursor()

print(f"📊 数据库: {db_path}")

# 查看所有表
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = cursor.fetchall()
print(f"\n📋 所有表:")
for table in tables:
    print(f"   {table[0]}")

# 查看dbbardata表
print(f"\n📊 dbbardata表统计:")
try:
    cursor.execute("SELECT COUNT(*) FROM dbbardata;")
    count = cursor.fetchone()[0]
    print(f"   总共有 {count} 条bar数据")
    
    # 查看所有symbol
    cursor.execute("SELECT DISTINCT symbol, exchange FROM dbbardata;")
    symbols = cursor.fetchall()
    print(f"   标的列表:")
    for symbol, exchange_code in symbols:
        cursor.execute("SELECT COUNT(*) FROM dbbardata WHERE symbol = ? AND exchange = ?", (symbol, exchange_code))
        cnt = cursor.fetchone()[0]
        try:
            exchange = Exchange(exchange_code)
        except:
            exchange = exchange_code
        print(f"     {symbol}.{exchange}: {cnt} 条")
        
        # 看一下时间范围
        cursor.execute("SELECT MIN(datetime), MAX(datetime) FROM dbbardata WHERE symbol = ? AND exchange = ?", (symbol, exchange_code))
        min_dt, max_dt = cursor.fetchone()
        print(f"     时间范围: {min_dt} ~ {max_dt}")
        
except Exception as e:
    print(f"❌ 查询失败: {e}")
    import traceback
    traceback.print_exc()

conn.close()
print("\n✅ 查询完成")
