[修复] pymysql execute 字面 % 冲突 - SQL 中 motion_% 的 % 被当参数占位符导致 /api/ui/videos 500, 改用 LEFT(filename,7)='motion_' 判断前缀

This commit is contained in:
ericwyuan
2026-08-22 13:06:52 +08:00
parent 6a29e88d86
commit 96644599d3

View File

@@ -114,7 +114,8 @@ def get_sync_videos(limit=15, offset=0, date_filter=None) -> List[Dict]:
cur = conn.cursor(pymysql.cursors.DictCursor)
# 录制时间优先,回退分析时间
date_expr = "COALESCE(NULLIF(event_start_time,''), processed_at, updated_at, created_at)"
content_filter = ("v.filename LIKE 'motion_%' "
# 注意pymysql execute 用 % 做参数占位符SQL 字面量不能含 %,故用 LEFT 判断 motion_ 前缀
content_filter = ("LEFT(v.filename, 7) = 'motion_' "
"OR EXISTS(SELECT 1 FROM sync_events se WHERE se.video_id = v.id)")
if date_filter:
cur.execute(
@@ -530,7 +531,7 @@ def get_sync_stats(date_str: str = None) -> Dict:
"""SELECT
(SELECT COUNT(*) FROM sync_videos sv
WHERE sv.status='done'
AND (sv.filename LIKE 'motion_%'
AND (LEFT(sv.filename, 7) = 'motion_'
OR EXISTS(SELECT 1 FROM sync_events se WHERE se.video_id=sv.id))
AND {d} LIKE %s) AS videos,
(SELECT COUNT(*) FROM sync_events se
@@ -545,7 +546,7 @@ def get_sync_stats(date_str: str = None) -> Dict:
"""SELECT
(SELECT COUNT(*) FROM sync_videos
WHERE status='done'
AND (filename LIKE 'motion_%'
AND (LEFT(filename, 7) = 'motion_'
OR EXISTS(SELECT 1 FROM sync_events se WHERE se.video_id=sync_videos.id))) AS videos,
(SELECT COUNT(*) FROM sync_events) AS events,
(SELECT COALESCE(SUM(is_attention_event),0) FROM sync_events) AS attention""")