-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
66 lines (53 loc) · 2.74 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
import sqlite3
class LibraryDB:
def __init__(self, database='library.db'):
self.connection = sqlite3.connect(database, check_same_thread=False)
self.cursor = self.connection.cursor()
def add_book(self, author_surname, initials, book_name, genre, cluster):
with self.connection:
self.cursor.execute('INSERT INTO Library(Author_surname, Author_initials, Name, Genre, Cluster)'
'VALUES(?, ?, ?, ?, ?)',
(author_surname, initials, book_name, genre, cluster))
return str(self.cursor.lastrowid)
def get_genres(self):
with self.connection:
genres = self.cursor.execute("SELECT DISTINCT Genre FROM Library").fetchall()
return genres
def get_surnames(self):
with self.connection:
surnames = self.cursor.execute("SELECT DISTINCT Author_surname FROM Library").fetchall()
return surnames
def get_book_names(self):
with self.connection:
book_names = self.cursor.execute("SELECT Name FROM Library").fetchall()
return book_names
def get_selectors(self, action):
with self.connection:
selectors = self.cursor.execute("SELECT DISTINCT ? FROM Library", (action,)).fetchall()
return selectors
def get_book_list_by_genre(self, genre):
with self.connection:
genres = self.cursor.execute('SELECT * FROM Library WHERE Genre=?', (genre,)).fetchall()
return genres
def get_book_list_by_book_names(self, book_name):
with self.connection:
books = self.cursor.execute('SELECT * FROM Library WHERE Name=?', (book_name,)).fetchall()
return books
def get_book_list_by_surnames(self, book_name):
with self.connection:
books = self.cursor.execute('SELECT * FROM Library WHERE Author_surname=?', (book_name,)).fetchall()
return books
def get_book_by_id(self, book_id):
with self.connection:
book = self.cursor.execute('SELECT * FROM Library WHERE id=?', (book_id,)).fetchone()
return book
def take_book(self, book_id, user_name, user_id):
with self.connection:
self.cursor.execute('UPDATE Library SET Owner=?, Owner_id=?, Cluster=NULL WHERE id=?', (user_name, user_id,
book_id))
def return_book(self, book_id, cluster):
with self.connection:
self.cursor.execute('UPDATE Library SET Owner=NULL, Owner_id=NULL, Cluster=? WHERE id=?', (cluster,
book_id))
def close(self):
self.connection.close()