check_h2_database.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 通过H2控制台检查数据库中的difficulty字段
  5. """
  6. import requests
  7. import json
  8. from urllib.parse import urlencode
  9. # H2控制台配置
  10. H2_CONSOLE_URL = "http://localhost:8080/h2-console"
  11. H2_LOGIN_URL = "http://localhost:8080/h2-console/login.do"
  12. H2_QUERY_URL = "http://localhost:8080/h2-console/query.do"
  13. def check_h2_database():
  14. """通过H2控制台检查数据库"""
  15. print("=== 检查H2数据库中的difficulty字段 ===")
  16. # 创建会话
  17. session = requests.Session()
  18. try:
  19. # 1. 访问H2控制台首页
  20. response = session.get(H2_CONSOLE_URL)
  21. if response.status_code != 200:
  22. print(f"✗ 无法访问H2控制台: {response.status_code}")
  23. return False
  24. print("✓ H2控制台可访问")
  25. # 2. 登录H2数据库
  26. login_data = {
  27. 'language': 'en',
  28. 'setting': 'Generic H2 (Embedded)',
  29. 'name': 'Generic H2 (Embedded)',
  30. 'driver': 'org.h2.Driver',
  31. 'url': 'jdbc:h2:mem:testdb',
  32. 'user': 'sa',
  33. 'password': ''
  34. }
  35. response = session.post(H2_LOGIN_URL, data=login_data)
  36. if 'query.do' not in response.text:
  37. print("✗ H2数据库登录失败")
  38. return False
  39. print("✓ H2数据库登录成功")
  40. # 3. 查询goal_detail表结构
  41. print("\n--- 查询goal_detail表结构 ---")
  42. structure_query = "SHOW COLUMNS FROM goal_detail"
  43. query_data = {
  44. 'sql': structure_query
  45. }
  46. response = session.post(H2_QUERY_URL, data=query_data)
  47. if response.status_code == 200:
  48. print("✓ 表结构查询成功")
  49. # 简单检查响应中是否包含difficulty字段
  50. if 'DIFFICULTY' in response.text.upper():
  51. print("✓ 发现DIFFICULTY字段")
  52. else:
  53. print("✗ 未发现DIFFICULTY字段")
  54. # 4. 查询goal_detail表数据
  55. print("\n--- 查询goal_detail表数据 ---")
  56. data_query = "SELECT id, goal_id, question, difficulty, created_at FROM goal_detail ORDER BY created_at DESC LIMIT 5"
  57. query_data = {
  58. 'sql': data_query
  59. }
  60. response = session.post(H2_QUERY_URL, data=query_data)
  61. if response.status_code == 200:
  62. print("✓ 数据查询成功")
  63. # 检查响应内容
  64. response_text = response.text
  65. if 'No data available' in response_text or '没有数据' in response_text:
  66. print(" 表中暂无数据")
  67. else:
  68. print(" 表中有数据,请查看H2控制台获取详细信息")
  69. # 尝试从响应中提取一些信息
  70. if 'null' in response_text.lower():
  71. print(" ⚠️ 响应中包含null值,可能存在difficulty字段为null的情况")
  72. # 5. 统计difficulty字段分布
  73. print("\n--- 统计difficulty字段分布 ---")
  74. stats_query = "SELECT difficulty, COUNT(*) as count FROM goal_detail GROUP BY difficulty"
  75. query_data = {
  76. 'sql': stats_query
  77. }
  78. response = session.post(H2_QUERY_URL, data=query_data)
  79. if response.status_code == 200:
  80. print("✓ 统计查询成功")
  81. if 'null' in response.text.lower():
  82. print(" ⚠️ 发现difficulty字段为null的记录")
  83. else:
  84. print(" difficulty字段统计完成")
  85. return True
  86. except Exception as e:
  87. print(f"✗ 检查过程出错: {e}")
  88. return False
  89. def main():
  90. """主函数"""
  91. print("开始检查H2数据库中的difficulty字段...")
  92. if check_h2_database():
  93. print("\n=== 检查完成 ===")
  94. print("详细信息请访问: http://localhost:8080/h2-console")
  95. print("连接信息:")
  96. print(" JDBC URL: jdbc:h2:mem:testdb")
  97. print(" 用户名: sa")
  98. print(" 密码: (空)")
  99. print("\n建议执行的SQL查询:")
  100. print("1. 查看表结构: SHOW COLUMNS FROM goal_detail")
  101. print("2. 查看数据: SELECT * FROM goal_detail ORDER BY created_at DESC LIMIT 10")
  102. print("3. 统计difficulty: SELECT difficulty, COUNT(*) FROM goal_detail GROUP BY difficulty")
  103. else:
  104. print("\n=== 检查失败 ===")
  105. print("请手动访问H2控制台进行检查")
  106. if __name__ == "__main__":
  107. main()