-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_connect.py
83 lines (60 loc) · 1.99 KB
/
db_connect.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'''
Connects to the PostgrSQL database in Docker
this used by exa_parse_for_sql, but can be used
on its own to check the database contents
'''
# pip install psycopg2-binary
import psycopg2
import set_constants
'''Make connection using psycopg2 - call from each function'''
def make_conn():
# creds = set_constants.get_db_creds()
# host = creds[0]
# user= creds[1]
# password=creds[2]
# database = creds[3]
try:
conn = psycopg2.connect(host='localhost', database='postgres',
user='postgres', password='postgres')
except Exception as e:
print(e)
exit(0)
return conn
def read_data():
'''read all from patient_info table'''
conn = make_conn()
if conn is not None:
print('Connection established to PostgreSQL.')
# Creating a cursor
cur = conn.cursor()
# Getting a query ready.
cur.execute('select * from patient_info;')
# we are fetching all the data from the query above.
get_all_data = cur.fetchall()
# Print all data
print(get_all_data)
# Close connection
cur.close()
else:
print('Connection not established to PostgreSQL.')
def read_div():
'''read div data from patient_info table'''
conn = make_conn()
if conn is not None:
print('Connection established to PostgreSQL.')
# Creating a cursor
cur = conn.cursor()
# Getting a query ready.
#cur.execute("select div from patient_info WHERE resourceType != 'Patient';")
cur.execute("select valueCoding from patient_info order by valueCoding ASC;")
# we are fetching all the data from the query above.
get_all_data = cur.fetchall()
# Print all data
print(get_all_data)
# Close connection
cur.close()
else:
print('Connection not established to PostgreSQL.')
if __name__=="__main__":
'''reads patient_info table'''
read_div()