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