import psycopg2
try:
conn=psycopg2.connect("host=localhost dbname=databasename user=postgres password=postgres")
mark=conn.cursor()
mquery="select * from table"
mark.execute(query)
# if it is selection query
row1=mark.fetchall()
# if there exists any record
if(mark.rowcount):
for row in row1:
print ( str( row[0] ) )
#Dont use like this.you need to catch specific exception.
#This will catch what ever exception arises
except:
print("Failed to connect to DB")
psycopg2 is a python module for connecting to Postgres database
No comments:
Post a Comment