check_difficulty_direct.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 直接通过HTTP请求检查H2控制台中的difficulty字段
  5. """
  6. import requests
  7. import re
  8. from urllib.parse import urlencode
  9. def check_difficulty_in_h2():
  10. """通过H2控制台检查difficulty字段"""
  11. print("=== 直接检查H2数据库中的difficulty字段 ===")
  12. session = requests.Session()
  13. try:
  14. # 1. 访问H2控制台
  15. console_url = "http://localhost:8080/h2-console"
  16. response = session.get(console_url)
  17. if response.status_code != 200:
  18. print(f"✗ 无法访问H2控制台: {response.status_code}")
  19. return False
  20. print("✓ H2控制台可访问")
  21. # 2. 提取JSESSIONID
  22. jsessionid = None
  23. for cookie in session.cookies:
  24. if cookie.name == 'JSESSIONID':
  25. jsessionid = cookie.value
  26. break
  27. if not jsessionid:
  28. print("✗ 无法获取JSESSIONID")
  29. return False
  30. print(f"✓ 获取到JSESSIONID: {jsessionid[:10]}...")
  31. # 3. 登录H2数据库
  32. login_url = "http://localhost:8080/h2-console/login.do"
  33. login_data = {
  34. 'language': 'en',
  35. 'setting': 'Generic H2 (Embedded)',
  36. 'name': 'Generic H2 (Embedded)',
  37. 'driver': 'org.h2.Driver',
  38. 'url': 'jdbc:h2:mem:testdb',
  39. 'user': 'sa',
  40. 'password': '',
  41. 'jsessionid': jsessionid
  42. }
  43. headers = {
  44. 'Content-Type': 'application/x-www-form-urlencoded',
  45. 'Referer': console_url
  46. }
  47. response = session.post(login_url, data=login_data, headers=headers)
  48. if response.status_code != 200:
  49. print(f"✗ 登录请求失败: {response.status_code}")
  50. return False
  51. # 检查是否登录成功
  52. if 'query.do' not in response.text and 'Query' not in response.text:
  53. print("✗ H2数据库登录失败")
  54. print("响应内容片段:", response.text[:200])
  55. return False
  56. print("✓ H2数据库登录成功")
  57. # 4. 执行查询
  58. query_url = "http://localhost:8080/h2-console/query.do"
  59. # 查询表结构
  60. print("\n--- 查询goal_detail表结构 ---")
  61. structure_query = "SHOW COLUMNS FROM goal_detail"
  62. query_data = {
  63. 'sql': structure_query,
  64. 'jsessionid': jsessionid
  65. }
  66. response = session.post(query_url, data=query_data, headers=headers)
  67. if response.status_code == 200:
  68. print("✓ 表结构查询成功")
  69. if 'DIFFICULTY' in response.text.upper():
  70. print("✓ 发现DIFFICULTY字段")
  71. else:
  72. print("✗ 未发现DIFFICULTY字段")
  73. print("响应内容片段:", response.text[:500])
  74. # 查询数据
  75. print("\n--- 查询goal_detail表数据 ---")
  76. data_query = "SELECT id, goal_id, question, difficulty, created_at FROM goal_detail ORDER BY created_at DESC LIMIT 5"
  77. query_data = {
  78. 'sql': data_query,
  79. 'jsessionid': jsessionid
  80. }
  81. response = session.post(query_url, data=query_data, headers=headers)
  82. if response.status_code == 200:
  83. print("✓ 数据查询成功")
  84. # 尝试解析HTML响应中的数据
  85. response_text = response.text
  86. # 查找表格数据
  87. if '<table' in response_text and '</table>' in response_text:
  88. print("✓ 找到查询结果表格")
  89. # 简单解析表格内容
  90. table_match = re.search(r'<table[^>]*>(.*?)</table>', response_text, re.DOTALL)
  91. if table_match:
  92. table_content = table_match.group(1)
  93. # 查找difficulty列的值
  94. if 'null' in table_content.lower():
  95. print("⚠️ 发现null值,可能存在difficulty字段为null的情况")
  96. # 查找具体的difficulty值
  97. difficulty_pattern = r'<td[^>]*>([^<]*)</td>'
  98. cells = re.findall(difficulty_pattern, table_content)
  99. print(f" 表格单元格数量: {len(cells)}")
  100. # 假设列顺序是: id, goal_id, question, difficulty, created_at
  101. if len(cells) >= 5:
  102. for i in range(0, len(cells), 5):
  103. if i + 4 < len(cells):
  104. id_val = cells[i].strip()
  105. goal_id = cells[i+1].strip()
  106. question = cells[i+2].strip()[:30] + "..." if len(cells[i+2]) > 30 else cells[i+2].strip()
  107. difficulty = cells[i+3].strip()
  108. created_at = cells[i+4].strip()
  109. print(f" 记录 {id_val}: difficulty = '{difficulty}'")
  110. if difficulty == '' or difficulty.lower() == 'null':
  111. print(f" ⚠️ 记录 {id_val} 的difficulty字段为空或null")
  112. else:
  113. print(f" ✓ 记录 {id_val} 的difficulty字段有值: {difficulty}")
  114. else:
  115. print("✗ 无法解析表格内容")
  116. else:
  117. print("✗ 响应中未找到表格")
  118. if 'No data available' in response_text:
  119. print(" 表中暂无数据")
  120. else:
  121. print(" 响应内容片段:", response_text[:300])
  122. # 统计difficulty字段分布
  123. print("\n--- 统计difficulty字段分布 ---")
  124. stats_query = "SELECT difficulty, COUNT(*) as count FROM goal_detail GROUP BY difficulty"
  125. query_data = {
  126. 'sql': stats_query,
  127. 'jsessionid': jsessionid
  128. }
  129. response = session.post(query_url, data=query_data, headers=headers)
  130. if response.status_code == 200:
  131. print("✓ 统计查询成功")
  132. response_text = response.text
  133. if '<table' in response_text:
  134. table_match = re.search(r'<table[^>]*>(.*?)</table>', response_text, re.DOTALL)
  135. if table_match:
  136. table_content = table_match.group(1)
  137. cells = re.findall(r'<td[^>]*>([^<]*)</td>', table_content)
  138. print(" difficulty字段分布:")
  139. for i in range(0, len(cells), 2):
  140. if i + 1 < len(cells):
  141. difficulty_val = cells[i].strip()
  142. count_val = cells[i+1].strip()
  143. if difficulty_val == '' or difficulty_val.lower() == 'null':
  144. print(f" NULL: {count_val} 条记录")
  145. else:
  146. print(f" '{difficulty_val}': {count_val} 条记录")
  147. return True
  148. except Exception as e:
  149. print(f"✗ 检查过程出错: {e}")
  150. import traceback
  151. traceback.print_exc()
  152. return False
  153. def main():
  154. """主函数"""
  155. print("开始直接检查H2数据库中的difficulty字段...")
  156. if check_difficulty_in_h2():
  157. print("\n=== 检查完成 ===")
  158. else:
  159. print("\n=== 检查失败 ===")
  160. print("请手动访问H2控制台进行检查:")
  161. print("访问地址: http://localhost:8080/h2-console")
  162. print("连接信息: jdbc:h2:mem:testdb, 用户名: sa, 密码: (空)")
  163. if __name__ == "__main__":
  164. main()