#!/usr/bin/env python3 """ 조제 데이터 상세 확인 - formula_id가 NULL인 케이스 분석 """ import sqlite3 def check_compound_details(): conn = sqlite3.connect('database/kdrug.db') cursor = conn.cursor() print("=" * 80) print("조제 데이터 상세 분석") print("=" * 80) # formula_id가 NULL인 조제건 확인 print("\n1. Formula ID가 NULL인 조제건:") print("-" * 80) cursor.execute(""" SELECT compound_id, patient_id, formula_id, is_custom, custom_type, custom_summary, compound_date, status FROM compounds WHERE formula_id IS NULL ORDER BY compound_id DESC """) null_formulas = cursor.fetchall() for comp in null_formulas: print(f" ID: {comp[0]}") print(f" 환자ID: {comp[1]}, is_custom: {comp[3]}, custom_type: {comp[4]}") print(f" custom_summary: {comp[5]}") print(f" 조제일: {comp[6]}, 상태: {comp[7]}") print() # 전체 조제 데이터 요약 print("\n2. 전체 조제 데이터 요약:") print("-" * 80) cursor.execute(""" SELECT COUNT(*) as total, COUNT(formula_id) as with_formula, COUNT(*) - COUNT(formula_id) as without_formula, SUM(CASE WHEN is_custom = 1 THEN 1 ELSE 0 END) as custom_count FROM compounds """) summary = cursor.fetchone() print(f" 총 조제건수: {summary[0]}") print(f" 처방 있음: {summary[1]}") print(f" 처방 없음(직접조제): {summary[2]}") print(f" 커스텀 플래그: {summary[3]}") # compound_items 테이블 확인 print("\n3. Compound_items 테이블 구조 및 데이터:") print("-" * 80) # 테이블 구조 확인 cursor.execute("PRAGMA table_info(compound_items)") columns = cursor.fetchall() if columns: print(" 테이블 구조:") for col in columns: print(f" {col[1]:20s} {col[2]:15s}") # 샘플 데이터 cursor.execute(""" SELECT * FROM compound_items LIMIT 5 """) items = cursor.fetchall() if items: print("\n 샘플 데이터:") for item in items: print(f" {item}") else: print(" compound_items 테이블이 비어있거나 없습니다.") # 직접조제의 약재 구성 확인 (compound_items와 연결) print("\n4. 직접조제(formula_id=NULL) 약재 구성:") print("-" * 80) for comp_id in [10, 8, 7]: # NULL인 compound_id들 cursor.execute(""" SELECT ci.*, h.herb_name FROM compound_items ci LEFT JOIN herb_items h ON ci.herb_item_id = h.herb_item_id WHERE ci.compound_id = ? LIMIT 5 """, (comp_id,)) items = cursor.fetchall() if items: print(f" Compound ID {comp_id}의 약재:") for item in items: print(f" {item}") else: print(f" Compound ID {comp_id}: 약재 데이터 없음") conn.close() if __name__ == "__main__": check_compound_details()