Python 链接PG数据库

python 使用 psycopg2 包 连接PG数据 对数据库中的额视图进行查询拿到查询后的数据值

import os,psycopg2 as pg
def conn_pg(dbname,host,port,usr,pw):
    conn = pg.connect(database=dbname,user=usr,password=pw,host=host,port=port)
    print("connected!!!!!")
    cur = conn.cursor()
    SQLstr = "SELECT mm,imo,lat,lon from inarea where mm='t'"
    cur.execute(SQLstr)
    lines = cur.fetchall()
    print(lines)
if __name__ == "__main__":
    conn_pg("BH_TEST", "localhost", 5432, "postgres", "root")

你可能感兴趣的:(python,SQL,数据库)