46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
|
|
#!/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()
|