获取数据库表蓝图,例如从 redshift 获取“describe table_name”命令,从 python 获取 DB2
Posted
技术标签:
【中文标题】获取数据库表蓝图,例如从 redshift 获取“describe table_name”命令,从 python 获取 DB2【英文标题】:Fetch DB tables blueprint like "describe table_name" commande from redshift and DB2 from python 【发布时间】:2019-01-01 12:48:47 【问题描述】:我想像使用describe [tableName] statement
一样使用我的python 代码获取数据。我想在 Redshift 和 DB2 上这样做。
我尝试使用 Pandas 和游标来做到这一点,我尝试了以下命令块:
"set search_path to SCHEMA; select * from pg_table_def where schemaname = 'schema' and LOWER(tablename) = 'TableName';
describe schema.tableName
select column_name, data_type, character_manimum_length from information_schema.columns where table_schema = 'Schema' and table_name = 'TableName';
\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
【问题讨论】:
【参考方案1】:很多此类数据都包含在SVV_COLUMNS
系统视图中。您可以使用table_name
和table_schema
列查询该表。
https://docs.aws.amazon.com/redshift/latest/dg/r_SVV_COLUMNS.html
【讨论】:
以上是关于获取数据库表蓝图,例如从 redshift 获取“describe table_name”命令,从 python 获取 DB2的主要内容,如果未能解决你的问题,请参考以下文章