Files
mini_code/models/add_tg_web_app_data_sqlite.py

46 lines
1.3 KiB
Python
Raw Permalink Normal View History

2026-02-13 22:59:13 +08:00
#!/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()