#!/usr/bin/env python3
"""Search entire DB for 唐山晟红 title"""
import sqlite3, sys
sys.stdout.reconfigure(encoding='utf-8')

DB_PATH = "/root/search.db"
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()

c.execute("SELECT page_url, title, script_name, length(content), substr(content,1,200) FROM gov_raw WHERE title LIKE '%晟红%' OR title LIKE '%,4-滴%' OR title LIKE '%滴·氨氯%'")
rows = c.fetchall()
if rows:
    for row in rows:
        print(f"Script: {row[2]}")
        print(f"Title: {row[1][:60]}")
        print(f"URL: {row[0]}")
        print(f"Content: {row[3]} chars")
        if row[3] > 20:
            print(f"Text: {row[4][:200]}")
        else:
            print(f"Text: {row[4]}")
        print()
else:
    print("全库未找到该标题")
    # Broader search
    c.execute("SELECT title FROM gov_raw WHERE title LIKE '%唐山%' ORDER BY id DESC LIMIT 10")
    rows2 = c.fetchall()
    if rows2:
        print("\n最近10条含'唐山'的标题:")
        for r in rows2:
            print(f"  {r[0][:80]}")

conn.close()
