#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 直接通过HTTP请求检查H2控制台中的difficulty字段 """ import requests import re from urllib.parse import urlencode def check_difficulty_in_h2(): """通过H2控制台检查difficulty字段""" print("=== 直接检查H2数据库中的difficulty字段 ===") session = requests.Session() try: # 1. 访问H2控制台 console_url = "http://localhost:8080/h2-console" response = session.get(console_url) if response.status_code != 200: print(f"✗ 无法访问H2控制台: {response.status_code}") return False print("✓ H2控制台可访问") # 2. 提取JSESSIONID jsessionid = None for cookie in session.cookies: if cookie.name == 'JSESSIONID': jsessionid = cookie.value break if not jsessionid: print("✗ 无法获取JSESSIONID") return False print(f"✓ 获取到JSESSIONID: {jsessionid[:10]}...") # 3. 登录H2数据库 login_url = "http://localhost:8080/h2-console/login.do" login_data = { 'language': 'en', 'setting': 'Generic H2 (Embedded)', 'name': 'Generic H2 (Embedded)', 'driver': 'org.h2.Driver', 'url': 'jdbc:h2:mem:testdb', 'user': 'sa', 'password': '', 'jsessionid': jsessionid } headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Referer': console_url } response = session.post(login_url, data=login_data, headers=headers) if response.status_code != 200: print(f"✗ 登录请求失败: {response.status_code}") return False # 检查是否登录成功 if 'query.do' not in response.text and 'Query' not in response.text: print("✗ H2数据库登录失败") print("响应内容片段:", response.text[:200]) return False print("✓ H2数据库登录成功") # 4. 执行查询 query_url = "http://localhost:8080/h2-console/query.do" # 查询表结构 print("\n--- 查询goal_detail表结构 ---") structure_query = "SHOW COLUMNS FROM goal_detail" query_data = { 'sql': structure_query, 'jsessionid': jsessionid } response = session.post(query_url, data=query_data, headers=headers) if response.status_code == 200: print("✓ 表结构查询成功") if 'DIFFICULTY' in response.text.upper(): print("✓ 发现DIFFICULTY字段") else: print("✗ 未发现DIFFICULTY字段") print("响应内容片段:", response.text[:500]) # 查询数据 print("\n--- 查询goal_detail表数据 ---") data_query = "SELECT id, goal_id, question, difficulty, created_at FROM goal_detail ORDER BY created_at DESC LIMIT 5" query_data = { 'sql': data_query, 'jsessionid': jsessionid } response = session.post(query_url, data=query_data, headers=headers) if response.status_code == 200: print("✓ 数据查询成功") # 尝试解析HTML响应中的数据 response_text = response.text # 查找表格数据 if '' in response_text: print("✓ 找到查询结果表格") # 简单解析表格内容 table_match = re.search(r']*>(.*?)', response_text, re.DOTALL) if table_match: table_content = table_match.group(1) # 查找difficulty列的值 if 'null' in table_content.lower(): print("⚠️ 发现null值,可能存在difficulty字段为null的情况") # 查找具体的difficulty值 difficulty_pattern = r']*>([^<]*)' cells = re.findall(difficulty_pattern, table_content) print(f" 表格单元格数量: {len(cells)}") # 假设列顺序是: id, goal_id, question, difficulty, created_at if len(cells) >= 5: for i in range(0, len(cells), 5): if i + 4 < len(cells): id_val = cells[i].strip() goal_id = cells[i+1].strip() question = cells[i+2].strip()[:30] + "..." if len(cells[i+2]) > 30 else cells[i+2].strip() difficulty = cells[i+3].strip() created_at = cells[i+4].strip() print(f" 记录 {id_val}: difficulty = '{difficulty}'") if difficulty == '' or difficulty.lower() == 'null': print(f" ⚠️ 记录 {id_val} 的difficulty字段为空或null") else: print(f" ✓ 记录 {id_val} 的difficulty字段有值: {difficulty}") else: print("✗ 无法解析表格内容") else: print("✗ 响应中未找到表格") if 'No data available' in response_text: print(" 表中暂无数据") else: print(" 响应内容片段:", response_text[:300]) # 统计difficulty字段分布 print("\n--- 统计difficulty字段分布 ---") stats_query = "SELECT difficulty, COUNT(*) as count FROM goal_detail GROUP BY difficulty" query_data = { 'sql': stats_query, 'jsessionid': jsessionid } response = session.post(query_url, data=query_data, headers=headers) if response.status_code == 200: print("✓ 统计查询成功") response_text = response.text if ']*>(.*?)', response_text, re.DOTALL) if table_match: table_content = table_match.group(1) cells = re.findall(r']*>([^<]*)', table_content) print(" difficulty字段分布:") for i in range(0, len(cells), 2): if i + 1 < len(cells): difficulty_val = cells[i].strip() count_val = cells[i+1].strip() if difficulty_val == '' or difficulty_val.lower() == 'null': print(f" NULL: {count_val} 条记录") else: print(f" '{difficulty_val}': {count_val} 条记录") return True except Exception as e: print(f"✗ 检查过程出错: {e}") import traceback traceback.print_exc() return False def main(): """主函数""" print("开始直接检查H2数据库中的difficulty字段...") if check_difficulty_in_h2(): print("\n=== 检查完成 ===") else: print("\n=== 检查失败 ===") print("请手动访问H2控制台进行检查:") print("访问地址: http://localhost:8080/h2-console") print("连接信息: jdbc:h2:mem:testdb, 用户名: sa, 密码: (空)") if __name__ == "__main__": main()