-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
67 lines (43 loc) · 1.71 KB
/
database.py
File metadata and controls
67 lines (43 loc) · 1.71 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
from database_connection import DatabaseConnection
"""
format of the cvs file
name,author,read\n
"""
def create_book_table():
with DatabaseConnection() as connection:
cursor = connection.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS books(name text primary key, author text, read integer)')
def prompt_add_book():
#",0); DROP TABLE books
name = input("Name of the book: ")
author = input("Name of the author: ")
with DatabaseConnection() as connection:
cursor = connection.cursor()
cursor.execute('INSERT INTO books VALUES(?,?,0)', (name, author))
def get_all_books():
with DatabaseConnection() as connection:
cursor = connection.cursor()
cursor.execute('SELECT * FROM books')
books = [{'name': row[0], 'author': row[1], 'read': row[2] } for row in cursor.fetchall()] #[(name,author,read), (name,author,read)]
return books
def list_book():
bookss = get_all_books()
for book in bookss:
read = 'Yes' if book['read'] else 'No'
print(f"{book['name']} by the {book['author']}. Is it read? {read}")
def prompt_read_book():
which_read = input("Which book did you read? ")
with DatabaseConnection() as connection:
cursor = connection.cursor()
cursor.execute('UPDATE books SET read=1 WHERE name=? ', (which_read,))
def prompt_delete_book():
which_delete = input("WHich book dou you want to delete? ")
with DatabaseConnection() as connection:
cursor = connection.cursor()
cursor.execute('DELETE FROM books WHERE name=? ', (which_delete,))
# prompt_add_book()
# prompt_add_book()
# list_book()
# prompt_read_book()
# prompt_delete_book()
# list_book()