modify
This commit is contained in:
40
aabook/src/alter_table.py
Normal file
40
aabook/src/alter_table.py
Normal file
@ -0,0 +1,40 @@
|
||||
import sqlite3
|
||||
import json
|
||||
import config
|
||||
import utils
|
||||
import logging
|
||||
import sys
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
# 连接 SQLite 数据库
|
||||
DB_PATH = config.global_sqlite_path # 替换为你的数据库文件
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
cursor = conn.cursor()
|
||||
|
||||
tbl_name_books = 'books'
|
||||
tbl_name_chapters_prefix = 'chapters'
|
||||
tbl_name_section = 'books_sections'
|
||||
|
||||
def add_columns_to_table(table_name):
|
||||
try:
|
||||
# 添加 words 字段
|
||||
add_words_column_query = f"ALTER TABLE {table_name} ADD COLUMN words INTEGER DEFAULT 0"
|
||||
cursor.execute(add_words_column_query)
|
||||
|
||||
# 添加 update_time 字段
|
||||
add_update_time_column_query = f"ALTER TABLE {table_name} ADD COLUMN update_time TEXT DEFAULT ('2000-01-01 00:00:00')"
|
||||
cursor.execute(add_update_time_column_query)
|
||||
|
||||
# 提交事务
|
||||
conn.commit()
|
||||
print(f"成功向表 {table_name} 中添加字段 words 和 update_time")
|
||||
except sqlite3.Error as e:
|
||||
print(f"添加字段时出现错误: {e}")
|
||||
|
||||
# 使用示例
|
||||
if __name__ == "__main__":
|
||||
# 循环遍历 0 到 100 的数字
|
||||
for i in range(100):
|
||||
table_name = f'{tbl_name_chapters_prefix}_{i}'
|
||||
add_columns_to_table(table_name)
|
||||
Reference in New Issue
Block a user