-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
57 lines (48 loc) · 2.01 KB
/
Copy pathdatabase.py
File metadata and controls
57 lines (48 loc) · 2.01 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
# database.py
import sqlite3
class Database:
def __init__(self, db_name):
self.conn = sqlite3.connect(db_name)
self.create_tables()
def create_tables(self):
cursor = self.conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS books (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
author TEXT,
description TEXT,
genre TEXT
)
''')
self.conn.commit()
def add_book(self, title, author, description, genre):
cursor = self.conn.cursor()
cursor.execute('INSERT INTO books (title, author, description, genre) VALUES (?, ?, ?, ?)',
(title, author, description, genre))
self.conn.commit()
def get_all_books(self):
cursor = self.conn.cursor()
cursor.execute('SELECT id, title, author FROM books')
return cursor.fetchall()
def get_book_details(self, book_id):
cursor = self.conn.cursor()
cursor.execute('SELECT * FROM books WHERE id = ?', (book_id,))
return cursor.fetchone()
def get_books_by_genre(self, genre):
cursor = self.conn.cursor()
cursor.execute('SELECT id, title, author FROM books WHERE genre = ?', (genre,))
return cursor.fetchall()
def search_books(self, keyword):
cursor = self.conn.cursor()
cursor.execute('SELECT id, title, author FROM books WHERE title LIKE ? OR author LIKE ? OR genre LIKE ?',
('%' + keyword + '%', '%' + keyword + '%', '%' + keyword + '%'))
return cursor.fetchall()
def delete_book(self, book_id):
cursor = self.conn.cursor()
cursor.execute('DELETE FROM books WHERE id = ?', (book_id,))
self.conn.commit()
def get_all_genres(self):
cursor = self.conn.cursor()
cursor.execute('SELECT DISTINCT genre FROM books')
return [item[0] for item in cursor.fetchall()]