Files
mini_code/models/add_tg_web_app_data_sqlite.py
Your Name e28338f34d HAHAHHA
2026-02-13 22:59:13 +08:00

46 lines
1.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
"""
为 SQLite haha.db 中 tg_phone_devices_copy1 表添加 tg_web_app_data 列(若不存在)。
解决 踢设备.py 报错: no such column: t1.tg_web_app_data
"""
import sys
from pathlib import Path
project_root = Path(__file__).resolve().parent.parent
if str(project_root) not in sys.path:
sys.path.insert(0, str(project_root))
from models.tg_phone_devices import db
TABLE = "tg_phone_devices_copy1"
COLUMN = "tg_web_app_data"
def column_exists_sqlite(cursor, table, column):
cursor.execute("PRAGMA table_info({})".format(table))
return any(row[1] == column for row in cursor.fetchall())
def main():
db.connect()
try:
with db.connection() as conn:
cursor = conn.cursor()
cursor.execute("PRAGMA table_info({})".format(TABLE))
if any(row[1] == COLUMN for row in cursor.fetchall()):
print(f"{TABLE} 已存在列 {COLUMN},无需添加。")
return
cursor.execute("ALTER TABLE {} ADD COLUMN {} TEXT".format(TABLE, COLUMN))
print(f"已在表 {TABLE}SQLite添加列 {COLUMN}")
finally:
if not db.is_closed():
try:
db.close()
except Exception:
pass
if __name__ == "__main__":
main()