#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Step 3: 매핑되지 않은 약재 수정 """ import sqlite3 def fix_mappings(): conn = sqlite3.connect('database/kdrug.db') cursor = conn.cursor() try: print("=== 약재 매핑 수정 ===\n") # 1. 백작약 → 작약으로 매핑 print("1. '백작약'을 '작약'으로 매핑...") # 작약의 주성분코드 확인 cursor.execute(""" SELECT ingredient_code FROM herb_masters WHERE herb_name = '작약' """) result = cursor.fetchone() if result: jaknyak_code = result[0] cursor.execute(""" UPDATE herb_items SET ingredient_code = ? WHERE herb_name = '백작약' """, (jaknyak_code,)) print(f" ✅ 백작약 → 작약 ({jaknyak_code}) 매핑 완료") else: print(" ⚠️ '작약'을 찾을 수 없습니다") # 2. 진피 매핑 print("\n2. '진피' 매핑 확인...") # 진피 관련 항목 찾기 cursor.execute(""" SELECT ingredient_code, herb_name FROM herb_masters WHERE herb_name LIKE '%진피%' """) jinpi_options = cursor.fetchall() if jinpi_options: print(f" 발견된 진피 관련 항목:") for code, name in jinpi_options: print(f" - {name} ({code})") # 첫 번째 항목으로 매핑 if len(jinpi_options) > 0: jinpi_code = jinpi_options[0][0] cursor.execute(""" UPDATE herb_items SET ingredient_code = ? WHERE herb_name = '진피' """, (jinpi_code,)) print(f" ✅ 진피 → {jinpi_options[0][1]} ({jinpi_code}) 매핑 완료") else: print(" ⚠️ '진피' 관련 항목을 찾을 수 없습니다") # 3. 최종 확인 print("\n3. 매핑 상태 확인...") cursor.execute(""" SELECT herb_name, ingredient_code FROM herb_items ORDER BY herb_name """) all_items = cursor.fetchall() mapped = 0 unmapped = [] for herb_name, code in all_items: if code: mapped += 1 else: unmapped.append(herb_name) print(f"\n📊 최종 매핑 결과:") print(f" • 전체: {len(all_items)}개") print(f" • 매핑됨: {mapped}개 ({mapped*100//len(all_items)}%)") print(f" • 미매핑: {len(unmapped)}개") if unmapped: print(f"\n ⚠️ 아직 매핑되지 않은 약재:") for name in unmapped: print(f" - {name}") # 4. 중요 약재들의 매핑 확인 print("\n4. 주요 약재 매핑 확인...") important_herbs = [ '건강', '감초', '당귀', '황기', '숙지황', '백출', '천궁', '육계', '인삼', '생강', '대추' ] for herb_name in important_herbs: cursor.execute(""" SELECT h.herb_name, h.ingredient_code, m.herb_name FROM herb_items h LEFT JOIN herb_masters m ON h.ingredient_code = m.ingredient_code WHERE h.herb_name = ? """, (herb_name,)) result = cursor.fetchone() if result and result[1]: print(f" ✅ {result[0]} → {result[1]} ({result[2]})") else: print(f" ❌ {herb_name} - 매핑 안 됨") conn.commit() print("\n✅ 매핑 수정 완료!") except Exception as e: print(f"❌ 오류 발생: {e}") conn.rollback() finally: conn.close() if __name__ == "__main__": fix_mappings()