survey/add_phone_column.py
2025-11-15 23:51:08 +08:00

40 lines
1.1 KiB
Python
Raw Permalink 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
# -*- coding: utf-8 -*-
import sqlite3
import os
def add_phone_column():
"""为现有的students表添加phone列"""
db_path = '/Users/moshui/Documents/survey/data/survey.db'
if not os.path.exists(db_path):
print(f"数据库不存在: {db_path}")
return
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
try:
# 检查phone列是否已存在
cursor.execute("PRAGMA table_info(students)")
columns = [column[1] for column in cursor.fetchall()]
if 'phone' not in columns:
# 添加phone列
cursor.execute("ALTER TABLE students ADD COLUMN phone TEXT")
print("已成功添加phone列到students表")
# 提交更改
conn.commit()
else:
print("phone列已存在无需添加")
except Exception as e:
print(f"添加phone列时出错: {e}")
conn.rollback()
finally:
conn.close()
if __name__ == "__main__":
add_phone_column()