Files
tophux_scrape/add_interested_field.py
xiaji 25da264413 第一次提交。
其中爬取是tophub_scraper.py
数据入库是 tophub_add_data_to_db.py
查看当前数据内容是 db_viewer.py
2025-11-09 17:20:44 +08:00

72 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
添加感兴趣标记字段脚本
为articles表添加is_interested字段默认值为0
"""
import sqlite3
import os
from loguru import logger
def add_interested_field():
"""为articles表添加is_interested字段"""
# 获取当前脚本所在目录的数据库文件路径
script_dir = os.path.dirname(os.path.abspath(__file__))
db_path = os.path.join(script_dir, "tophub_data.db")
# 检查数据库文件是否存在
if not os.path.exists(db_path):
logger.error(f"数据库文件不存在: {db_path}")
return False
try:
# 连接数据库
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# 检查is_interested字段是否已存在
cursor.execute("PRAGMA table_info(articles)")
columns = cursor.fetchall()
column_names = [column[1] for column in columns]
if "is_interested" in column_names:
logger.info("is_interested字段已存在无需添加")
conn.close()
return True
# 添加is_interested字段默认值为0
logger.info("正在添加is_interested字段...")
cursor.execute("ALTER TABLE articles ADD COLUMN is_interested INTEGER DEFAULT 0")
# 提交更改
conn.commit()
logger.info("成功添加is_interested字段")
# 验证字段是否添加成功
cursor.execute("PRAGMA table_info(articles)")
columns = cursor.fetchall()
column_names = [column[1] for column in columns]
if "is_interested" in column_names:
logger.info("验证成功is_interested字段已添加到articles表")
else:
logger.error("验证失败is_interested字段未成功添加")
conn.close()
return False
conn.close()
return True
except sqlite3.Error as e:
logger.error(f"数据库操作出错: {str(e)}")
return False
except Exception as e:
logger.error(f"添加字段时出错: {str(e)}")
return False
if __name__ == "__main__":
logger.add("db_modify.log", rotation="10 MB", level="INFO")
if add_interested_field():
logger.info("数据库修改完成")
else:
logger.error("数据库修改失败")