-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRead.py
108 lines (93 loc) · 2.92 KB
/
Read.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import psycopg2
from config import config
def id_extraction(rs):
l = []
for i in rs:
# converting set to list
k = list(i)
# taking the first element from the list and append it to the list
l.append(k[0])
# Converting integer list to string list
s = [str(i) for i in l]
# Join list items using join()
res = int("".join(s))
return res
def final_report():
""" query data from the hospitals table """
sql = "SELECT hospital_id, hospital_name FROM hospitals ORDER BY " \
"distance asc, beds, oxy_tanks"
conn = None
try:
params = config()
conn = psycopg2.connect(**params)
cur = conn.cursor()
cur.execute(sql)
print("The Order of Approach: ", cur.rowcount)
row = cur.fetchone()
while row is not None:
print(row)
row = cur.fetchone()
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
def read_hospital_one(ID):
# read number of beds available in a hospital
sql = """SELECT hospital_name, beds FROM hospitals "
"WHERE hospital_id = '%s'"""
conn = None
updated_rows = 0
try:
# read database configuration
params = config()
# connect to the PostgreSQL database
conn = psycopg2.connect(**params)
# create a new cursor
cur = conn.cursor()
# execute the UPDATE statement
cur.execute(sql, [ID])
result = cur.fetchall()
# get the number of updated rows
updated_rows = cur.rowcount
# Commit the changes to the database
conn.commit()
# Close communication with the PostgreSQL database
cur.close()
return result
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
return updated_rows
def read_hospital_two(ID):
# read number of oxygen tanks available in a hospital
sql = """SELECT hospital_name, oxy_tanks FROM hospitals "
"WHERE hospital_id = %s"""
conn = None
updated_rows = 0
try:
# read database configuration
params = config()
# connect to the PostgreSQL database
conn = psycopg2.connect(**params)
# create a new cursor
cur = conn.cursor()
# execute the UPDATE statement
cur.execute(sql, [ID])
result = cur.fetchall()
# get the number of updated rows
updated_rows = cur.rowcount
# Commit the changes to the database
conn.commit()
# Close communication with the PostgreSQL database
cur.close()
return result
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
return updated_rows