-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
173 lines (152 loc) · 4.8 KB
/
database.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/python
import psycopg2
import config
def create_tables():
""" create tables in the PostgreSQL database"""
command = """
CREATE TABLE subscriptions (
user_id VARCHAR(255) NOT NULL,
serie_id VARCHAR(255) NOT NULL,
chat_id INTEGER NOT NULL,
serie_name VARCHAR(255),
PRIMARY KEY (user_id , serie_id)
)
"""
conn = None
try:
# read the connection parameters
params = config.params
# connect to the PostgreSQL server
conn = psycopg2.connect(**params)
cur = conn.cursor()
# create table one by one
# cur.execute("DROP TABLE subscriptions;")
# cur.close()
# conn.commit()
# conn = psycopg2.connect(**params)
# cur = conn.cursor()
cur.execute(command)
# close communication with the PostgreSQL database server
cur.close()
# commit the changes
conn.commit()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
def insert_register(user_id, serie_id, chat_id, serie_name):
sql = """INSERT INTO subscriptions(user_id, serie_id, chat_id, serie_name)
VALUES(%s, %s, %s, %s) RETURNING serie_id;"""
conn = None
chat_id = int(chat_id)
response = None
try:
# read the connection parameters
params = config.params
# connect to the PostgreSQL server
conn = psycopg2.connect(**params)
# create a new cursor
cur = conn.cursor()
# execute the INSERT statement
cur.execute(sql, (user_id, serie_id, chat_id, serie_name))
# get the generated id back
response = cur.fetchone()[0]
# commit the changes to the database
conn.commit()
# close communication with the database
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
return response
def remove_register(user_id, serie_id):
sql = """DELETE FROM subscriptions
WHERE user_id = '%s' AND serie_id = '%s';"""
conn = None
response = None
try:
# read the connection parameters
params = config.params
# connect to the PostgreSQL server
conn = psycopg2.connect(**params)
# create a new cursor
cur = conn.cursor()
# execute the INSERT statement
cur.execute(sql, (user_id, serie_id))
# get the generated id back
response = True
# commit the changes to the database
conn.commit()
# close communication with the database
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
return response
def get_registers(serie_id):
conn = None
result = []
serie_id = int(serie_id)
try:
# read the connection parameters
params = config.params
# connect to the PostgreSQL server
conn = psycopg2.connect(**params)
cur = conn.cursor()
cur.execute("SELECT chat_id FROM subscriptions WHERE serie_id = '%s'" % serie_id)
row = cur.fetchone()
while row is not None:
result.append(row[0])
row = cur.fetchone()
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
return result
def search(user_id, serie_id):
conn = None
result = None
try:
# read the connection parameters
params = config.params
# connect to the PostgreSQL server
conn = psycopg2.connect(**params)
cur = conn.cursor()
cur.execute("SELECT serie_name FROM subscriptions WHERE user_id = '%s' AND serie_id = '%s'" % (user_id, serie_id))
row = cur.fetchone()
result = row
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
return result
def get_subscriptions(user_id):
conn = None
result = []
try:
# read the connection parameters
params = config.params
# connect to the PostgreSQL server
conn = psycopg2.connect(**params)
cur = conn.cursor()
cur.execute("SELECT serie_name FROM subscriptions WHERE user_id = '%s'" % user_id)
row = cur.fetchone()
while row is not None:
result.append(row[0])
row = cur.fetchone()
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
return result