-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasic_Book_Tracker.py
36 lines (31 loc) · 1.21 KB
/
Basic_Book_Tracker.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
import mysql.connector
def create_table():
db = mysql.connector.connect(host="localhost", user="root", password="Gande", database="MyBooks")
cursor = db.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS books (id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255), author VARCHAR(255))")
db.close()
def add_book(title, author):
db = mysql.connector.connect(host="localhost", user="root", password="Gande", database="MyBooks")
cursor = db.cursor()
cursor.execute("INSERT INTO books (title, author) VALUES (%s, %s)", (title, author))
db.commit()
db.close()
def view_books():
db = mysql.connector.connect(host="localhost", user="root", password="Gande", database="MyBooks")
cursor = db.cursor()
cursor.execute("SELECT * FROM books")
books = cursor.fetchall()
db.close()
return books
def delete_book(id):
db = mysql.connector.connect(host="localhost", user="root", password="Gande", database="MyBooks")
cursor = db.cursor()
cursor.execute("DELETE FROM books WHERE id=%s", (id,))
db.commit()
db.close()
create_table()
add_book('To Kill a Mockingbird', 'Harper Lee')
add_book('1984', 'George Orwell')
print(view_books())
delete_book(1)
print(view_books())