从redshift和从python中获取DB2蓝图,例如“ describe table_name” commande

时间:2019-01-01 12:48:48

标签: python pandas db2 cursor amazon-redshift

我想像使用 gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (clickcheck.get(position).equals(true)) { //complete some task then position=position+1; clickcheck.remove(position); clickcheck.add(position,true); } else { Toast.makeText(getActivity(), "Click On Level "+position, Toast.LENGTH_SHORT).show(); } 一样使用我的python代码获取数据。我想在Redshift和DB2上做到这一点。

我尝试使用熊猫和光标来做到这一点,我尝试了以下命令块:

  1. describe [tableName] statement

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

  3. describe schema.tableName

  4. 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()

1 个答案:

答案 0 :(得分:0)

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

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