diff --git a/aabook/src/alter_table.py b/aabook/src/alter_table.py index 28dc1df..78fba71 100644 --- a/aabook/src/alter_table.py +++ b/aabook/src/alter_table.py @@ -4,6 +4,7 @@ import config import utils import logging import sys +import argparse from datetime import datetime @@ -38,6 +39,49 @@ def alter_chapters_table(): table_name = f'{tbl_name_chapters_prefix}_{i}' add_columns_to_table(table_name) + +# 检查脏数据 +def check_dirty_chapters(): + # 循环遍历 0 到 100 的数字 + dirty_chapters_all = 0 + dirty_chapters = 0 + for i in range(100): + table_name = f'{tbl_name_chapters_prefix}_{i}' + try: + cursor.execute(f"SELECT count(*) FROM {table_name} WHERE created_at >= '2025-03-23 10:20:00' and created_at <= '2025-03-23 11:20:00' ") + row = cursor.fetchone() + if row: + dirty_chapters_all += row[0] + + cursor.execute(f"SELECT count(*) FROM {table_name} WHERE created_at >= '2025-03-23 10:20:00' and created_at <= '2025-03-23 11:20:00' and content like '%aabook%' ") + row = cursor.fetchone() + if row: + dirty_chapters += row[0] + + except sqlite3.Error as e: + print(f"query error: {e}") + + print(f"all: {dirty_chapters_all}, count: {dirty_chapters}") + + +# 检查脏数据 +def update_dirty_chapters(): + # 循环遍历 0 到 100 的数字 + for i in range(100): + table_name = f'{tbl_name_chapters_prefix}_{i}' + try: + cursor.execute(f"update {table_name} set has_content = 0 WHERE created_at >= '2025-03-23 10:20:00' and created_at <= '2025-03-23 11:20:00' ") + updated_rows = cursor.rowcount + print(f"update {table_name}, affected rows: {updated_rows}") + + conn.commit() + + #cursor.execute(f"update {table_name} set has_content = 0 WHERE created_at >= '2025-03-23 10:20:00' and created_at <= '2025-03-23 11:20:00' and content like '%aabook%' ") + #conn.commit() + + except sqlite3.Error as e: + print(f"query error: {e}") + # 检查是否存在,已存在的先删除 def check_view_exist(view_name): cursor.execute("SELECT name FROM sqlite_master WHERE type='view' AND name=?", (view_name,)) @@ -126,6 +170,37 @@ def create_views(): create_finished_view() create_finished_books_info_view() -# 使用示例 + + +# 建立缩写到函数的映射 +function_map = { + "query" : check_dirty_chapters, + "update" : update_dirty_chapters, + "views" : create_views, + "alter" : alter_chapters_table, +} + +# 主函数 +def main(cmd): + # 执行指定的函数 + if cmd: + function_names = args.cmd.split(",") # 拆分输入 + for short_name in function_names: + func = function_map.get(short_name.strip()) # 从映射中获取对应的函数 + if callable(func): + func() + else: + print(f"{short_name} is not a valid function shortcut.") + else: # 全量执行 + print('wrong cmd.') + + if __name__ == "__main__": - create_views() \ No newline at end of file + # 命令行参数处理 + keys_str = ",".join(function_map.keys()) + + parser = argparse.ArgumentParser(description='alter books.db') + parser.add_argument("--cmd", type=str, help=f"Comma-separated list of function shortcuts: {keys_str}") + args = parser.parse_args() + + main(args.cmd)