-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
225 lines (158 loc) · 9.04 KB
/
database.py
File metadata and controls
225 lines (158 loc) · 9.04 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import sqlite3
def check_existing_user(username): # check if username already exists
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("SELECT COUNT(*) FROM users WHERE username=?", [username])
return cursor.fetchone()[0]
def create_user(username, password): # create new user
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("INSERT INTO users VALUES (?, ?)", [username, password])
def login_user(username, password): # check that user with given username and password exists
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("SELECT COUNT(*) FROM users WHERE username=? AND password=?", [username, password])
return cursor.fetchone()[0]
def insert_book(isbn, title, author, published, description):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("INSERT INTO books VALUES (?, ?, ?, ?, ?)", [isbn, title, author, published, description])
def insert_game(title, publisher, developer, release, description):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("INSERT INTO games VALUES (?, ?, ?, ?, ?, ?)", [None, title, publisher, developer, release, description])
def insert_movie(title, director, release, description):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("INSERT INTO movies VALUES (?, ?, ?, ?, ?)", [None, title, director, release, description])
def get_books(page):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("SELECT * FROM books")
return cursor.fetchall()[page * 4 - 4:page * 4]
def get_books_count():
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("SELECT COUNT(*) FROM books")
return cursor.fetchone()[0]
def get_games(page):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("SELECT * FROM games")
return cursor.fetchall()[page * 4 - 4:page * 4]
def get_games_count():
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("SELECT COUNT(*) FROM games")
return cursor.fetchone()[0]
def get_movies(page):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("SELECT * FROM movies")
return cursor.fetchall()[page * 4 - 4:page * 4]
def get_movies_count():
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("SELECT COUNT(*) FROM movies")
return cursor.fetchone()[0]
def check_book_already_added(username, isbn):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("SELECT COUNT(*) FROM userbooks WHERE username=? AND isbn=?", [username, isbn])
return cursor.fetchone()[0]
def check_game_already_added(username, gid):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("SELECT COUNT(*) FROM usergames WHERE username=? AND gid=?", [username, gid])
return cursor.fetchone()[0]
def check_movie_already_added(username, mid):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("SELECT COUNT(*) FROM usermovies WHERE username=? AND mid=?", [username, mid])
return cursor.fetchone()[0]
# insert queries for usergames, books, movies
def insert_user_game(username, gid):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("INSERT INTO usergames VALUES(?,?,?)", [username, gid, 0])
def insert_user_movie(username, mid):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("INSERT INTO usermovies VALUES(?,?,?)", [username, mid, 0])
def insert_user_book(username, isbn):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("INSERT INTO userbooks VALUES(?,?,?)", [username, isbn, 0])
def update_user_book_rating(username, isbn, rating):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("UPDATE userbooks SET rating=? WHERE username=? AND isbn=?", [rating, username, isbn])
def update_user_game_rating(username, gid, rating):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("UPDATE usergames SET rating=? WHERE username=? AND gid=?", [rating, username, gid])
def update_user_movie_rating(username, mid, rating):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("UPDATE usermovies SET rating=? WHERE username=? AND mid=?", [rating, username, mid])
# delete an entity from their specified cat collection
def delete_user_game(username, gid):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("DELETE FROM usergames WHERE username=? AND gid=?", [username, gid])
def delete_user_movie(username, mid):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("DELETE FROM usermovies WHERE username=? AND mid=?", [username, mid])
def delete_user_book(username, isbn):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("DELETE FROM userbooks WHERE username=? AND isbn=?", [username, isbn])
# search queries/by movie titles/game title/ isbn/book title
def search_books(search, page):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("SELECT * FROM books WHERE btitle LIKE ? OR isbn LIKE ?", ['%' + search + '%', '%' + search + '%'])
return cursor.fetchall()[page * 4 - 4: page * 4], len(cursor.fetchall())
def search_games(search, page):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("SELECT * FROM games WHERE gtitle LIKE ?", ['%' + search + '%'])
return cursor.fetchall()[page * 4 - 4: page * 4], len(cursor.fetchall())
def search_movies(search, page):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("SELECT * FROM movies WHERE mtitle LIKE ?", ['%' + search + '%'])
return cursor.fetchall()[page * 4 - 4: page * 4], len(cursor.fetchall())
# collection filtered by cat of movie/game/books to display titles to user of their collection ordered by rating
def get_user_games(username, page):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("SELECT games.gid, games.gtitle, games.publisher, games.developer, games.grelease, games.gdescription, usergames.rating FROM games, usergames WHERE games.gid=usergames.gid AND usergames.username=?", [username])
return cursor.fetchall()[page * 4 - 4:page * 4]
def get_user_movies(username, page):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("SELECT movies.mid, movies.mtitle, movies.director, movies.mrelease, movies.mdescription, usermovies.rating FROM movies, usermovies WHERE movies.mid=usermovies.mid AND usermovies.username=?", [username])
return cursor.fetchall()[page * 4 - 4:page * 4]
def get_user_books(username, page):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("SELECT books.isbn, books.btitle, books.author, books.published, books.bdescription, userbooks.rating FROM books, userbooks WHERE books.isbn=userbooks.isbn AND userbooks.username=?", [username])
return cursor.fetchall()[page * 4 - 4:page * 4]
# count total media collection by three cat and altogether
def get_user_books_count(username):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("SELECT COUNT(*) FROM userbooks WHERE username=?", [username])
return cursor.fetchone()[0]
def get_user_games_count(username):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("SELECT COUNT(*) FROM usergames WHERE username=?", [username])
return cursor.fetchone()[0]
def get_user_movies_count(username):
with sqlite3.connect('proj4.db') as connection:
cursor = connection.cursor()
cursor.execute("SELECT COUNT(*) FROM usermovies WHERE username=?", [username])
return cursor.fetchone()[0]