-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgettables.py
40 lines (31 loc) · 1.05 KB
/
gettables.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import psycopg2
from config import dbconfig
def gettable():
""" Connect to the PostgreSQL database server """
conn = None
try:
# read connection parameters
params = dbconfig()
# connect to the PostgreSQL server
conn = psycopg2.connect(**params)
# create a cursor
cur = conn.cursor()
print("The database is live.")
# execute and comit the sql statement
with open("gettable.sql", 'r') as f:
cur.execute(f.read())
conn.commit()
# since there is only table in the database fetchone() method is used. If there
# more than one table, then use fetchall() method.
for table in cur.fetchone():
print(table)
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
# close the cursor with the PostgreSQL
cur.close()
# close the connection with the PostgreSQL
conn.close()
print("The database is closed.")
gettable()