-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathdb.py
72 lines (53 loc) · 1.58 KB
/
db.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
import sqlite3
import datetime
def create_table():
db = sqlite3.connect('database.db')
query = """
CREATE TABLE if not exists BOOKS
(ID INTEGER PRIMARY KEY AUTOINCREMENT,
NAME TEXT NOT NULL,
CREATED_AT DATETIME default current_timestamp,
COMPLETED_AT DATATIME
)
"""
cur = db.cursor()
cur.execute(query)
db.close()
create_table()
def insert_book(name, completed_at):
db = sqlite3.connect('database.db')
query = """
INSERT INTO BOOKS(NAME, COMPLETED_AT)
VALUES (?,?)
"""
cur = db.cursor()
cur.execute(query, (name, completed_at))
db.commit()
db.close()
print('completed')
def get_all_books():
db = sqlite3.connect('database.db')
statement = 'SELECT id, name, completed_at FROM BOOKS'
cur = db.cursor()
items_io = cur.execute(statement)
item_lst = [i for i in items_io]
return item_lst
# insert_book('Time, fast or slow', datetime.datetime.now())
def add_book(self):
title = self.title_input.text()
if title:
cursor.execute("INSERT INTO books (title) VALUES (?)", (title,))
conn.commit()
self.title_input.clear()
self.load_books()
def delete_book(book_id):
# Connect to the SQLite database
db = sqlite3.connect('database.db')
# Define the SQL query to delete a book with a specific ID
query = "DELETE FROM books WHERE id = ?"
# Execute the query with the provided book ID as a parameter
db.execute(query, (book_id,))
# Commit the changes to the database
db.commit()
# Close the database connection
db.close()