#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
在 ads 表中添加 pub 字段
"""

import sqlite3

# 数据库路径
db_path = r'\\ll\D\xtrssvjj\db\dpd.db'

# 连接数据库
conn = sqlite3.connect(db_path)
cursor = conn.cursor()

# 检查 ads 表是否存在
try:
    cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='ads';")
    table_exists = cursor.fetchone()

    if table_exists:
        # 查看 ads 表当前结构
        print("ads 表当前结构:")
        cursor.execute("PRAGMA table_info(ads);")
        columns = cursor.fetchall()
        for col in columns:
            print(f"- {col[1]}: {col[2]}")
        
        # 检查 pub 字段是否已存在
        pub_exists = any(col[1] == 'pub' for col in columns)
        
        if not pub_exists:
            # 添加 pub 字段
            cursor.execute("ALTER TABLE ads ADD COLUMN pub INTEGER DEFAULT 0;")
            conn.commit()
            print("\n成功添加 pub 字段到 ads 表")
            
            # 再次查看结构确认
            print("\n添加后的 ads 表结构:")
            cursor.execute("PRAGMA table_info(ads);")
            columns = cursor.fetchall()
            for col in columns:
                print(f"- {col[1]}: {col[2]}")
        else:
            print("\npub 字段已存在")
    else:
        print("ads 表不存在")

except Exception as e:
    print(f"错误: {e}")
finally:
    conn.close()
