python pandas 读取postgresql中的数据

    import psycopg2
    import pandas as pd
    
    # postgres config
    postgres_host = ""               # 数据库地址
    postgres_port = "5432"       # 数据库端口
    postgres_user = ""              # 数据库用户名
    postgres_password = ""      # 数据库密码
    postgres_datebase = ""      # 数据库名字
    postgres_table1 = ""           #数据库中的表的名字

    # connection string
    conn_string = "host=" + postgres_host + " port=" + postgres_port + " dbname=" + postgres_datebase + \
                  " user=" + postgres_user + " password=" + postgres_password
    conn = psycopg2.connect(conn_string)

    sql_command1 = "select * from" .format(postgres_table1)

    try:
        data1 = pd.read_sql(sql_command1, conn)
    except:
        print("load data from postgres failure !")
        exit()

    if data1.shape[0] == 0:
        print("there is no data in !")
import psycopg2

pg_host = ""
pg_port = "5433"
pg_user = ""
pg_password = ""
pg_database = ""
pg_table = ""

conn_string = "host=" + pg_host + " port=" + pg_port + " dbname=" + pg_database + " user=" + pg_user + " password=" + pg_password
conn = psycopg2.connect(conn_string)
curs = conn.cursor()

sql_command = "select {} from {} where "
curs.execute(sql_command)
data = pd.DataFrame(curs.fetchall(), columns=select_columns)

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值