import dmPython

# 数据库连接配置
config = {
    "user": "DAM_CLOUD",  # 替换为你的用户名
    "password": "DAM_CLOUD",  # 替换为你的密码
    "host": "192.168.0.22",  # 替换为数据库主机地址，如localhost等
    "port": 5555,  # 替换为达梦数据库的端口号，默认一般是5236
}


def execute_query(sql):
    conn = None
    result11 = None
    try:
        conn = dmPython.connect(**config)
        cursor = conn.cursor()
        cursor.execute(sql)
        result11 = cursor.fetchall()
    except dmPython.DatabaseError as e:
        print(f"Database error: {e}")
    except Exception as e:
        print(f"Error: {e}")
    finally:
        if cursor:
            cursor.close()
        if conn:
            conn.close()
    return result11


def execute_update(sql):
    conn = None
    try:
        conn = dmPython.connect(**config)
        cursor = conn.cursor()
        cursor.execute(sql)
        conn.commit()  # 提交事
    except dmPython.DatabaseError as e:
        print(f"Database error: {e}")
    except Exception as e:
        print(f"Error: {e}")
    finally:
        if cursor:
            cursor.close()
        if conn:
            conn.close()


if __name__ == "__main__":
    excsql = (
        f" select COLUMN_COMMENT,DICT_TYPE from DAM_CLOUD.INFRA_CODEGEN_COLUMN WHERE 1=1 and DELETED=0 limit 10; ")
    results = execute_query(excsql)
    sql_update = "UPDATE DAM_CITY.CITYPROD_UNIVERSITY SET LATITUDE='213' WHERE id=1892102544369856513;"
    execute_update(sql_update)
    print(results)
