1
This commit is contained in:
91
add_industry_column.py
Normal file
91
add_industry_column.py
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
修复脚本:确保 gsdh_data 表有 industry_company 列
|
||||||
|
"""
|
||||||
|
|
||||||
|
import psycopg2
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
def load_config():
|
||||||
|
"""加载数据库配置"""
|
||||||
|
if os.path.exists("config.json"):
|
||||||
|
with open("config.json", "r", encoding="utf-8") as f:
|
||||||
|
return json.load(f)
|
||||||
|
return {}
|
||||||
|
|
||||||
|
def safe_add_column(conn, table, col, dtype):
|
||||||
|
"""安全地添加列(如果不存在)"""
|
||||||
|
cur = conn.cursor()
|
||||||
|
try:
|
||||||
|
# 首先检查列是否存在
|
||||||
|
cur.execute("""
|
||||||
|
SELECT column_name FROM information_schema.columns
|
||||||
|
WHERE table_name = %s AND column_name = %s
|
||||||
|
""", (table, col))
|
||||||
|
|
||||||
|
if not cur.fetchone():
|
||||||
|
cur.execute(f"ALTER TABLE {table} ADD COLUMN {col} {dtype}")
|
||||||
|
print(f"✅ 添加列 {col} 到 {table} 表")
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
print(f"ℹ️ 列 {col} 已存在于 {table} 表")
|
||||||
|
return False
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ 添加列 {col} 时出错: {e}")
|
||||||
|
return False
|
||||||
|
finally:
|
||||||
|
cur.close()
|
||||||
|
|
||||||
|
def main():
|
||||||
|
config = load_config()
|
||||||
|
|
||||||
|
if not config.get('db_host'):
|
||||||
|
print("❌ 未找到数据库配置")
|
||||||
|
return
|
||||||
|
|
||||||
|
print(f"🔗 连接到数据库: {config.get('db_host')}:{config.get('db_port')}/{config.get('db_name')}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
conn = psycopg2.connect(
|
||||||
|
host=config.get("db_host", "localhost"),
|
||||||
|
port=config.get("db_port", "5432"),
|
||||||
|
user=config.get("db_user", "gsdh"),
|
||||||
|
password=config.get("db_password", "123gsdh"),
|
||||||
|
database=config.get("db_name", "gsdh"),
|
||||||
|
connect_timeout=5
|
||||||
|
)
|
||||||
|
|
||||||
|
# 确保 gsdh_data 表有 industry_company 列
|
||||||
|
print("\n📋 检查 gsdh_data 表结构...")
|
||||||
|
added = safe_add_column(conn, 'gsdh_data', 'industry_company', 'VARCHAR(200)')
|
||||||
|
|
||||||
|
if added:
|
||||||
|
conn.commit()
|
||||||
|
print("\n✅ 数据库修复成功!")
|
||||||
|
print(" 现在签到功能应该可以正常工作了。")
|
||||||
|
else:
|
||||||
|
print("\n✅ 数据库已经是最新状态,无需修复")
|
||||||
|
|
||||||
|
# 显示当前表结构
|
||||||
|
print("\n📊 当前 gsdh_data 表的列:")
|
||||||
|
cur = conn.cursor()
|
||||||
|
cur.execute("""
|
||||||
|
SELECT column_name, data_type
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_name = 'gsdh_data'
|
||||||
|
ORDER BY ordinal_position
|
||||||
|
""")
|
||||||
|
for col in cur.fetchall():
|
||||||
|
print(f" - {col[0]}: {col[1]}")
|
||||||
|
cur.close()
|
||||||
|
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"\n❌ 连接数据库失败: {e}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
10
config.json
10
config.json
@@ -1,9 +1,9 @@
|
|||||||
{
|
{
|
||||||
"event_title": "AI共生大会",
|
"event_title": "面对面行业联合分享",
|
||||||
"event_sub_title": "云南AI大会",
|
"event_sub_title": "3月特别期",
|
||||||
"event_time": "等待输入",
|
"event_time": "2026.3.27 19:30-22:00",
|
||||||
"event_location": "玉溪青花街三生咖啡酒吧",
|
"event_location": "百里春秋文化空间二楼小剧场",
|
||||||
"event_content": "等待输入",
|
"event_content": "当增长不再靠规模,企业还能怎么活",
|
||||||
"primary_color": "#00f2ff",
|
"primary_color": "#00f2ff",
|
||||||
"secondary_color": "#0066ff",
|
"secondary_color": "#0066ff",
|
||||||
"bg_color": "#050814",
|
"bg_color": "#050814",
|
||||||
|
|||||||
78
fix_industry_column.py
Normal file
78
fix_industry_column.py
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
修复数据库脚本:为 gsdh_data 表添加缺失的 industry_company 列
|
||||||
|
"""
|
||||||
|
|
||||||
|
import psycopg2
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
def load_config():
|
||||||
|
"""加载数据库配置"""
|
||||||
|
if os.path.exists("config.json"):
|
||||||
|
with open("config.json", "r", encoding="utf-8") as f:
|
||||||
|
return json.load(f)
|
||||||
|
return {}
|
||||||
|
|
||||||
|
def fix_industry_column():
|
||||||
|
"""添加 industry_company 列到 gsdh_data 表"""
|
||||||
|
config = load_config()
|
||||||
|
|
||||||
|
print(f"连接到数据库: {config.get('db_host')}:{config.get('db_port')}/{config.get('db_name')}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
conn = psycopg2.connect(
|
||||||
|
host=config.get("db_host", "localhost"),
|
||||||
|
port=config.get("db_port", "5432"),
|
||||||
|
user=config.get("db_user", "gsdh"),
|
||||||
|
password=config.get("db_password", "123gsdh"),
|
||||||
|
database=config.get("db_name", "gsdh"),
|
||||||
|
connect_timeout=5
|
||||||
|
)
|
||||||
|
cur = conn.cursor()
|
||||||
|
|
||||||
|
# 检查列是否存在
|
||||||
|
cur.execute("""
|
||||||
|
SELECT column_name
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_name = 'gsdh_data' AND column_name = 'industry_company'
|
||||||
|
""")
|
||||||
|
|
||||||
|
if cur.fetchone():
|
||||||
|
print("✅ industry_company 列已存在,无需修复")
|
||||||
|
else:
|
||||||
|
# 添加列
|
||||||
|
print("🔧 正在添加 industry_company 列...")
|
||||||
|
cur.execute("""
|
||||||
|
ALTER TABLE gsdh_data
|
||||||
|
ADD COLUMN industry_company VARCHAR(200)
|
||||||
|
""")
|
||||||
|
conn.commit()
|
||||||
|
print("✅ industry_company 列已成功添加")
|
||||||
|
|
||||||
|
# 验证列存在
|
||||||
|
print("\n当前 gsdh_data 表的列:")
|
||||||
|
cur.execute("""
|
||||||
|
SELECT column_name, data_type
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_name = 'gsdh_data'
|
||||||
|
ORDER BY ordinal_position
|
||||||
|
""")
|
||||||
|
columns = cur.fetchall()
|
||||||
|
for col in columns:
|
||||||
|
print(f" - {col[0]}: {col[1]}")
|
||||||
|
|
||||||
|
cur.close()
|
||||||
|
conn.close()
|
||||||
|
print("\n✨ 修复完成!")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"\n❌ 错误: {e}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
fix_industry_column()
|
||||||
1
main.py
1
main.py
@@ -1525,6 +1525,7 @@ def init_database():
|
|||||||
cur.execute(f"ALTER TABLE {table} ADD COLUMN {col} {dtype}")
|
cur.execute(f"ALTER TABLE {table} ADD COLUMN {col} {dtype}")
|
||||||
print(f"Added column {col} to {table}")
|
print(f"Added column {col} to {table}")
|
||||||
|
|
||||||
|
safe_add_column('gsdh_data', 'industry_company', 'VARCHAR(200)')
|
||||||
safe_add_column('gsdh_data', 'fee', 'VARCHAR(50)')
|
safe_add_column('gsdh_data', 'fee', 'VARCHAR(50)')
|
||||||
safe_add_column('gsdh_data', 'payment_channel', 'VARCHAR(50)')
|
safe_add_column('gsdh_data', 'payment_channel', 'VARCHAR(50)')
|
||||||
safe_add_column('gsdh_data', 'out_trade_no', 'VARCHAR(100)')
|
safe_add_column('gsdh_data', 'out_trade_no', 'VARCHAR(100)')
|
||||||
|
|||||||
Reference in New Issue
Block a user