check_db.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import sqlite3
  2. from datetime import datetime
  3. import os
  4. # 找到最新的数据库文件
  5. data_dir = 'data'
  6. db_files = [f for f in os.listdir(data_dir) if f.startswith('trading_data_') and f.endswith('.db')]
  7. db_files.sort(reverse=True)
  8. if not db_files:
  9. print("没有找到数据库文件")
  10. exit(1)
  11. db_path = os.path.join(data_dir, db_files[0])
  12. print(f"使用数据库: {db_path}")
  13. conn = sqlite3.connect(db_path)
  14. cursor = conn.cursor()
  15. # 先查看所有表
  16. print('\n=== 数据库中的表 ===')
  17. cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
  18. tables = cursor.fetchall()
  19. for table in tables:
  20. print(f" - {table[0]}")
  21. # 查看price_data表的结构
  22. print('\n=== price_data 表结构 ===')
  23. try:
  24. cursor.execute("PRAGMA table_info(price_data)")
  25. columns = cursor.fetchall()
  26. for col in columns:
  27. print(f" {col[1]}: {col[2]}")
  28. except Exception as e:
  29. print(f" 错误: {e}")
  30. print('\n=== Price Data (最新10条) ===')
  31. try:
  32. cursor.execute('SELECT * FROM price_data ORDER BY timestamp DESC LIMIT 10')
  33. rows = cursor.fetchall()
  34. for row in rows:
  35. ts = datetime.fromtimestamp(row[1]).strftime('%Y-%m-%d %H:%M:%S')
  36. print(f"所有列: {row}")
  37. print(f" 时间: {ts}")
  38. print(f" ID: {row[0]}, 时间戳: {row[1]}, 会话ID: {row[2]}, 标的: {row[3]}")
  39. print(f" Lighter价格: {row[4]}, Binance价格: {row[5]}, 价差(bps): {row[6]}")
  40. print()
  41. except Exception as e:
  42. print(f" 错误: {e}")
  43. print('\n=== Trading Events (最新10条) ===')
  44. cursor.execute('SELECT * FROM trading_events ORDER BY timestamp DESC LIMIT 10')
  45. rows = cursor.fetchall()
  46. if rows:
  47. for row in rows:
  48. ts = datetime.fromtimestamp(row[1]).strftime('%Y-%m-%d %H:%M:%S')
  49. print(f"所有列: {row}")
  50. print(f" 时间: {ts}")
  51. print(f" ID: {row[0]}, 时间戳: {row[1]}, 会话ID: {row[2]}, 标的: {row[3]}")
  52. print(f" 事件类型: {row[4]}, 价格: {row[5]}, 数量: {row[6]}, 策略状态: {row[7]}")
  53. print(f" 价差(bps): {row[8]}, 成功: {row[9]}, 错误信息: {row[10]}")
  54. print()
  55. else:
  56. print(" (无数据)")
  57. print('\n=== 数据统计 ===')
  58. cursor.execute('SELECT COUNT(*) FROM price_data')
  59. print(f"价格数据总数: {cursor.fetchone()[0]}")
  60. cursor.execute('SELECT COUNT(*) FROM trading_events')
  61. print(f"交易事件总数: {cursor.fetchone()[0]}")
  62. conn.close()