Python导出SqlServerl数据字典为excel

sql代码

sql 复制代码
SELECT
	tableName = D.name ,
    tableIntroduce = isnull(F.value,
''),
    sort = A.colorder,
    fieldName = A.name,
        catogary = B.name,
        bytes = A.Length,
        lengths = COLUMNPROPERTY(A.id,
A.name,
'PRECISION'),
        scales = isnull(COLUMNPROPERTY(A.id,
A.name,
'Scale'),
0),
        isOrNotNull =
Case
	When A.isnullable = 1 Then '√'
	Else ''
End,
            primarays =
Case
	When exists(
	SELECT
		1
	FROM
		sysobjects
	Where
		xtype = 'PK'
		and parent_obj = A.id
		and name in (
		SELECT
			name
		FROM
			sysindexes
		WHERE
			indid in(
			SELECT
				indid
			FROM
				sysindexkeys
			WHERE
				id = A.id
				AND colid = A.colid))) then '√'
	else ''
end,
        defauts = isnull(E.Text,
''),
            annotations = isnull(G.[value],
'')
FROM
        syscolumns A
Left Join
        systypes B
    On
        A.xusertype = B.xusertype
Inner Join
        sysobjects D
        On
        A.id = D.id
and D.xtype = 'U'
and D.name <> 'dtproperties'
Left Join
        syscomments E
 on
        A.cdefault = E.id
Left Join
    sys.extended_properties G
    on
        A.id = G.major_id
and A.colid = G.minor_id
Left Join
    sys.extended_properties F
    On
        D.id=F.major_id and F.minor_id=0
        --where d.name='BigTable'    --如果只查询指定表,加上此条件
    Order By
        A.id,A.colorder 

python 代码

python 复制代码
#2023-01-29 22:30:35.660
#已通过
from datetime import datetime
import os
import pymssql as pymssql
import xlwt


def getData():
    connect = pymssql.connect('192.168.121.130', 'sa', 'Aa123456789', 'jiradb')
    if connect:
            print("连接成功!")
    cur = connect.cursor()
    query = """ SELECT
	tableName = D.name ,
    tableIntroduce = isnull(F.value,
''),
    sort = A.colorder,
    fieldName = A.name,
        catogary = B.name,
        bytes = A.Length,
        lengths = COLUMNPROPERTY(A.id,
A.name,
'PRECISION'),
        scales = isnull(COLUMNPROPERTY(A.id,
A.name,
'Scale'),
0),
        isOrNotNull =
Case
	When A.isnullable = 1 Then '√'
	Else ''
End,
            primarays =
Case
	When exists(
	SELECT
		1
	FROM
		sysobjects
	Where
		xtype = 'PK'
		and parent_obj = A.id
		and name in (
		SELECT
			name
		FROM
			sysindexes
		WHERE
			indid in(
			SELECT
				indid
			FROM
				sysindexkeys
			WHERE
				id = A.id
				AND colid = A.colid))) then '√'
	else ''
end,
        defauts = isnull(E.Text,
''),
            annotations = isnull(G.[value],
'')
FROM
        syscolumns A
Left Join
        systypes B
    On
        A.xusertype = B.xusertype
Inner Join
        sysobjects D
        On
        A.id = D.id
and D.xtype = 'U'
and D.name <> 'dtproperties'
Left Join
        syscomments E
 on
        A.cdefault = E.id
Left Join
    sys.extended_properties G
    on
        A.id = G.major_id
and A.colid = G.minor_id
Left Join
    sys.extended_properties F
    On
        D.id=F.major_id and F.minor_id=0
        --where d.name='BigTable'    --如果只查询指定表,加上此条件
    Order By
        A.id,A.colorder """

    cur.execute(query)
    data = cur.fetchall()  # 元组类型
    return data


def exportExcel(name):
    data = getData()
    myExcel = xlwt.Workbook('encoding=utf-8')
    # 定义表的宽
    sheet1 = myExcel.add_sheet(name, cell_overwrite_ok=True)
    sheet1.col(0).width = 300 * 20
    sheet1.col(1).width = 400 * 20
    sheet1.col(2).width = 100 * 20
    sheet1.col(3).width = 300 * 20
    sheet1.col(4).width = 256 * 20
    sheet1.col(5).width = 180 * 20
    sheet1.col(6).width = 180 * 20
    sheet1.col(7).width = 100 * 20
    sheet1.col(8).width = 100 * 20
    sheet1.col(9).width = 100 * 20
    sheet1.col(10).width = 180 * 20
    sheet1.col(11).width = 800 * 20

    # 设置居中
    a1 = xlwt.Alignment()
    a1.horz = 0x02
    a1.vert = 0x01
    style = xlwt.XFStyle()  # 赋值style为XFStyle为初始化样式
    style.alignment = a1

    today = datetime.today(
    )  # 获取当前日期,得到一个datetime对象如:(2019, 7, 2, 23, 12, 23, 424000)
    today_date = datetime.date(today)  # 将获取到的datetime对象仅取日期如:2019-7-2
    items = [
        '数据表', '表名', '字段序号', '字段', '类型', '占用字节数', '长度', '小数点', '是否为空', '是否为主键',
        '默认值', '注释'
    ]
    for col in range(len(items)):
        sheet1.write(0, col, items[col])
    # 合并第二列的name,从content获取第一列数据,[("Choleen","xxx"),()]
    first_col = []
    for i in range(len(data)):
        first_col.append(data[i][0])
    print("first_col:", first_col)
    # 去掉重复的列数据,并顺序不变
    nFirst_col = list(set(first_col))
    nFirst_col.sort(key=first_col.index)
    print("nFirst_col:", nFirst_col)
    row = 1
    for i in nFirst_col:
        count = first_col.count(i)  # 计算重复的元素个数
        mergeRow = row + count - 1  # 合并后的上行数,
        sheet1.write_merge(row, mergeRow, 0, 0, i, style)  # 第一列
        sheet1.write_merge(row, mergeRow, 1, 1, i, style)
        row = mergeRow + 1  # 从下一行开始写入

    # 获取data[i]中的第二个元素,循环写入
    for row in range(len(data)):
        for col in range(1, len(data[row])):
            result = data[row][col]
            str = typeof(result)  # 获取类型
            if str == None:  # 不能识别的类型,需要转换
                result = result.decode('utf-8')
            sheet1.write(row + 1, col, result, style)

    fileName = name + '.xls'
    rootPath = os.path.dirname(os.path.abspath('ExportSqlServer.py')) + '\\'
    print(rootPath)
    flag = os.path.exists(rootPath + fileName)
    if flag:
        os.remove(rootPath + fileName)
        myExcel.save(fileName)
    else:
        myExcel.save(fileName)


def typeof(variate):
    type = None
    if isinstance(variate, int):
        type = "int"

    elif isinstance(variate, str):
        type = "str"
    elif isinstance(variate, float):
        type = "float"
    elif isinstance(variate, list):
        type = "list"
    elif isinstance(variate, tuple):
        type = "tuple"
    elif isinstance(variate, dict):
        type = "dict"
    elif isinstance(variate, set):
        type = "set"
    return type


if __name__ == '__main__':
    print("这是sqlServer导出的数据字典")
    # response = chardet.detect(b'\xe7\x94\xa8\xe6\x88\xb7\xe8\xa1\xa8')
    # print(response)
    exportExcel("user表")

遇到报错,连接字符串密码当时填写错了

相关推荐
这个DBA有点耶1 小时前
NULL不是空——数据库里最反直觉的设计,90%新人踩过的坑
数据库·mysql·代码规范
用户8356290780511 小时前
Python 实现 PDF 文件加密与解密方法
后端·python
用户8356290780511 小时前
使用 Python 冻结与拆分 Excel 窗格教程
后端·python
这个DBA有点耶3 小时前
AI写的SQL跑崩了生产库,这锅谁背?
数据库·人工智能·程序员
镜舟科技3 小时前
Databricks 再提 LTAP,AI 时代的数据底座为何重回大一统叙事?
数据库·架构·agent
Databend4 小时前
从湖仓升级为 Agent 时代的数据控制面,Snowflake 和 Databricks 有哪些布局
大数据·数据库·agent
ClouGence7 小时前
SQL Server CDC 能放到 Always On 备库读吗?一文讲透原理与实践
数据库·sql server
你好潘先生9 小时前
别再记命令了,用 yeero do 说句人话就能跑脚本,而且不烧 token
服务器·python·命令行
Agent_大师10 小时前
WebSocket 行情重连成功,K线缺口不会自动消失
python
荣码10 小时前
LLM结构化输出:让AI返回JSON而不是废话,我踩了4个坑
java·python