python/ pandas/ db2/ cursor/ amazon-redshift

I want to fetch data using my python code like we do with describe [tableName] statement . I want to do that on Redshift and DB2.

I tried to do that using Pandas and cursors, I tried the following chunks of commands:

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

I want the output as following:

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

A lot of this data is included in the SVV_COLUMNS system view. You can query that table using the table_name and table_schema columns.

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

暂无
暂无

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Insert into DB2 table from a python dataframe Can I combine python's `for` statement with SQL like this: `for id, name, ctime in db.select('table_name', where='…')` Pandas - describe table from DB - big data Update Table in IBM DB2 Cloud from Python notebook Timestamp in db2 from string in python Cannot connect to Db2 from python Exporting data from DB2 using Python SQLite and Python - Fetch value from DB code to insert values into db2 table in python how to read a db2 table into python pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM