栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Python

SQL语句--获取数据库表信息,表名、列名、描述注释等

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SQL语句--获取数据库表信息,表名、列名、描述注释等

        information_schema详解

import pandas as pd
import re
import psycopg2
from sqlalchemy import create_engine

# 连接库账户信息
gongsi_engine =  psycopg2.connect(dbname='kk_dw',user='*******',password='*****',host='***')
# 从公司数据库读取需要的数据表
table_data = pd.read_sql(r"select * from pg_tables where schemaname = 'dwd';",con=gongsi_engine)
table_data.head()
data = pd.read_sql(r"""select * 
                         from information_schema.columns 
                        where table_schema='dwd' 
                              and table_name='dwd_store_other_in_storage_orders_detail'""",con=gongsi_engine)
data[['table_name','column_name','udt_name']]

        获取描述等 

data = pd.read_sql(r"""select 
    a.attnum,
    n.nspname,
    c.relname,
    a.attname as field,
    t.typname as type,
    a.attlen as length,
    a.atttypmod as lengthvar,
    a.attnotnull as notnull,
    b.description as comment 
from
    pg_namespace n left join pg_class c
    on n.oid = c.relnamespace
    left join pg_attribute a 
    on a.attrelid = c.oid
    left join pg_description b 
    on a.attrelid = b.objoid and a.attnum = b.objsubid
    left join pg_type t 
    on a.atttypid = t.oid
where
    n.nspname = 'dwd'       --table_schema
    and c.relname = 'dwd_store_o2o_sales_detail' -- table_name
    and a.attnum > 0   
order by
    a.attnum;""",con=gongsi_engine)

         这样实现的功能,是我能批量获取数据库里面的表名与字段以及字段描述等信息。

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1038065.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号