简体   繁体   中英

Unable to fetch rows from PostGresSQL table

I'm trying to fetch data from a table in the PostGreSQL database. I'm able to query the results using PostGreSQL but when I try to do so with Python using psycopg2 module, it doesn't return anything. The curse object returned is of type "None" and is non iterable.

Here's my code:

import psycopg2 as p
con = p.connect(database="Scheduling", user="postgres", password="test", host="127.0.0.1", 
port="5432")
cur = con.cursor()
df = cur.execute(''' SELECT * FROM  public."HOME" ''').fetchall()
print(type(df))

Here's the error message: Traceback (most recent call last): File "C:/Users/adi.jakka/PycharmProjects/Flask/TEST.PY", line 4, in df = cur.execute(''' SELECT * FROM public."HOME" ''').fetchall() AttributeError: 'NoneType' object has no attribute 'fetchall'

Process finished with exit code 1

This should work:

import psycopg2 as p
con = p.connect(database="Scheduling", user="postgres", password="test", host="127.0.0.1", 
port="5432")
cur = con.cursor()
cur.execute(''' SELECT * FROM  public."HOME" ''')
df = cur.fetchall()
print(df)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM