繁体   English   中英

从redshift和从python提取DB表蓝图,例如“ describe table_name” commande

[英]Fetch DB tables blueprint like “describe table_name” commande from redshift and DB2 from python

我想使用我的python代码来获取数据,就像我们对describe [tableName] statement所做的那样。 我想在Redshift和DB2上做到这一点。

我尝试使用Pandas和游标执行此操作,并尝试了以下命令块:

  1. "set search_path to SCHEMA; select * from pg_table_def where schemaname = 'schema' and LOWER(tablename) = 'TableName';

  2. describe schema.tableName

  3. select column_name, data_type, character_manimum_length from information_schema.columns where table_schema = 'Schema' and table_name = 'TableName';

  4. \\d or \\d+

import psycopg2
import numpy as np
import pandas as pd   



con=psycopg2.connect(dbname= 'DBNAME', host="Host-Link",
    port= '5439', user= 'username', password= 'password')
    print(con)

cur = con.cursor()
query  = "set search_path to Schema; select * from pg_table_def where schemaname = 'Schema' and LOWER(tablename) = 'TableName';"
cur.execute(query)
temp = cur.fetchall()
print(temp)

data_frame = pd.read_sql("set search_path to Schema; select * from pg_table_def where schemaname = 'Schema' and LOWER(tablename) = 'TableName';", con)
print(data_frame)

con.close()

我想要的输出如下:

COLUMN_NAME DATA_TYPE   PK  NULLABLE    DEFAULT AUTOINCREMENT   COMPUTED    REMARKS POSITION
col1        varchar(10) YES NO            NO          NO           1
col2        varchar(50) NO  NO            NO          NO           2
col3        smallint    NO  NO            NO          NO           3

SVV_COLUMNS系统视图中包含许多此类数据。 您可以使用table_nametable_schema列查询该表。

https://docs.aws.amazon.com/redshift/latest/dg/r_SVV_COLUMNS.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM