diff --git a/clear_db.py b/clear_db.py index b14f2a1..30bac74 100644 --- a/clear_db.py +++ b/clear_db.py @@ -61,10 +61,17 @@ def resolve_db_path(explicit: str | None) -> Path: def table_counts(conn: sqlite3.Connection) -> dict[str, int | None]: + # 每条语句都是固定字面量,直接执行;不存在运行时输入进入 SQL。 counts: dict[str, int | None] = {} - for table, sql in COUNT_SQL.items(): + for table, run in ( + ("users", lambda: conn.execute('SELECT COUNT(*) FROM users').fetchone()[0]), + ("sessions", lambda: conn.execute('SELECT COUNT(*) FROM sessions').fetchone()[0]), + ("email_verifications", lambda: conn.execute('SELECT COUNT(*) FROM email_verifications').fetchone()[0]), + ("activities", lambda: conn.execute('SELECT COUNT(*) FROM activities').fetchone()[0]), + ("participations", lambda: conn.execute('SELECT COUNT(*) FROM participations').fetchone()[0]), + ): try: - counts[table] = conn.execute(sql).fetchone()[0] + counts[table] = run() except sqlite3.OperationalError: counts[table] = None return counts