-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.py
More file actions
144 lines (115 loc) · 5.85 KB
/
scripts.py
File metadata and controls
144 lines (115 loc) · 5.85 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import psycopg2
import random
from configparser import ConfigParser
def config(filename='database.ini', section='postgresql'):
parser = ConfigParser()
parser.read(filename)
db = {}
if parser.has_section(section):
params = parser.items(section)
for param in params:
db[param[0]] = param[1]
else:
raise Exception('Section {0} not found in the {1} file'.format(section, filename))
return db
config = config()
connection = psycopg2.connect(**config)
def open_connection():
try:
cursor = connection.cursor()
# Print PostgreSQL Connection properties
print ( connection.get_dsn_parameters(),"\n")
# Print PostgreSQL version
cursor.execute("SELECT version();")
record = cursor.fetchone()
print("You are connected to - ", record,"\n")
except (Exception, psycopg2.Error) as error:
print ("Error while connecting to PostgreSQL", error)
connection.rollback()
#finally:
#closing database connection.
def query(query):
try:
cursor = connection.cursor()
connection.cursor().execute("SET SCHEMA '{}'".format('airbnb'))
cursor.execute(query)
connection.commit()
fetched = cursor.fetchall()
return fetched
except (Exception) as error:
connection.rollback()
return error
cursor.close()
def uploadlisting(name, email, location, duration, maxsize, language, description, rentaltype, rules, cost, availability, contact):
#test query, note the type values. uploadlisting("testbnb", "test@test.com", "ottawa", 365, 2, "english", "fun place", "no pets", "300", True, "jeff")
try:
cursor = connection.cursor()
connection.cursor().execute("SET SCHEMA '{}'".format('airbnb'))
bnbid=random.randint(1,10000)
print(bnbid)
cursor.execute("INSERT INTO bnb (bnbid, email, name, location, duration, maxsize, Language, description, rentaltype, rules, cost, availability, contact) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", (bnbid, email, name, location, duration, maxsize, [language], description, rentaltype, rules, cost, availability, contact))
connection.commit()
cursor.close()
except (Exception, psycopg2.Error) as error :
print("exception - " , error)
connection.rollback()
def availability(name):
cursor = connection.cursor()
connection.cursor().execute("SET SCHEMA '{}'".format('airbnb'))
query = "SELECT availability, cost FROM bnb WHERE name = '{}'".format(name)
cursor.execute(query)
availability = cursor.fetchall()
string = ""
for row in availability:
string += "availability = {}".format(row[0]) + " , " + "cost = {}".format( row[1])
print(string)
return string
def occupancyrate(bnbid):
cursor = connection.cursor()
connection.cursor().execute("SET SCHEMA '{}'".format('airbnb'))
query = "SELECT duration FROM bnb WHERE bnbid = '{}'".format(bnbid)
cursor.execute(query)
duration = cursor.fetchone()
return "occupancy rate: {:.2f}%".format((duration[0]/365)*100)
def close():
if(connection):
connection.close()
print("PostgreSQL connection is closed")
def addhost(email, firstname, lastname, middlename, language, gender, govid, phonenum, address, emergencycontact, dob, staylist, adventurelist, experiencelist):
try:
cursor = connection.cursor()
connection.cursor().execute("SET SCHEMA '{}'".format('airbnb'))
cursor.execute("INSERT INTO person (email, firstname, lastname, middlename, language, gender, govid, phonenum, address, emergencycontact, dob) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", (email, firstname, lastname, middlename, [language], gender, govid, phonenum, address, emergencycontact, dob) )
cursor.execute("INSERT INTO host (email, staylist, adventurelist, experiencelist) VALUES (%s, %s, %s, %s)", (email,[staylist],[adventurelist],[experiencelist]))
connection.commit()
cursor.close()
# anotherone(email, staylist, adventurelist, experiencelist)
except (Exception) as error :
print("exception - " , error)
connection.rollback()
def edithost(email, firstname, lastname, middlename, language, gender, govid, phonenum, address, emergencycontact, dob, staylist, adventurelist, experiencelist):
try:
cursor = connection.cursor()
connection.cursor().execute("SET SCHEMA '{}'".format('airbnb'))
cursor.execute("DELETE FROM host WHERE email = '{}'".format(email))
cursor.execute("DELETE FROM person WHERE email = '{}'".format(email))
cursor.execute("INSERT INTO person (email, firstname, lastname, middlename, language, gender, govid, phonenum, address, emergencycontact, dob) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", (email, firstname, lastname, middlename, [language], gender, govid, phonenum, address, emergencycontact, dob) )
cursor.execute("INSERT INTO host (email, staylist, adventurelist, experiencelist) VALUES (%s, %s, %s, %s)", (email,[staylist],[adventurelist],[experiencelist]))
connection.commit()
cursor.close()
except (Exception) as error :
print("exception - " , error)
connection.rollback()
def searchbnb(searchtype, searchparam):
try:
cursor = connection.cursor()
connection.cursor().execute("SET SCHEMA '{}'".format('airbnb'))
cursor.execute("SELECT * FROM BNB WHERE {} = '{}'".format(searchtype, searchparam))
availability = cursor.fetchall()
return availability
except (Exception) as error :
print("exception - " , error)
connection.rollback()
# if __name__ == "__main__":
#edithost("hosttest@test.com", "testchange", "test", "middle", "english", "Male", "123456", "1231231234", "doe doe doe st.", "123456789", "1920-01-01", "962", "1234", "1234")
# print(str(searchbnb("location", "ottawa")))