#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""风电行业修正: other → power (FTS title 匹配), 无限重试挤调度器锁间隙"""
import sqlite3
import time

SQL = '''
UPDATE gov_raw SET industry='power' WHERE industry='other' AND id IN (
  SELECT g.rowid FROM gov_search g WHERE g.gov_search MATCH 'title:风电场 OR title:风力发电 OR title:海上风电 OR title:风电项目'
)
'''

attempt = 0
while True:
    attempt += 1
    try:
        conn = sqlite3.connect('/mnt/data/search.db', timeout=290)
        conn.execute('PRAGMA busy_timeout=290000')
        cur = conn.execute(SQL)
        n = cur.rowcount
        conn.commit()
        conn.close()
        print(f"第{attempt}次尝试成功: 更新 {n} 条")
        break
    except sqlite3.OperationalError as e:
        conn.close()
        print(f"第{attempt}次尝试 locked ({e}), 30s 后重试", flush=True)
        time.sleep(30)
