|
| 1 | +from tkinter import * |
| 2 | +import pymysql |
| 3 | +from tkinter import messagebox |
| 4 | +from connect_db import connect |
| 5 | + |
| 6 | + |
| 7 | +def add_books(home, add_book): |
| 8 | + add_book.configure(background="black") |
| 9 | + Label(add_book, text="Add Book", bg="black", fg="white", font=("Courier", 35)).pack( |
| 10 | + pady=15 |
| 11 | + ) |
| 12 | + Label(add_book, text="Book Id", bg="black", fg="white", font=("Courier", 25)).pack( |
| 13 | + pady=5 |
| 14 | + ) |
| 15 | + id = Text(add_book, height=1, width=30, font=("Courier", 25)) |
| 16 | + id.pack(pady=5) |
| 17 | + Label(add_book, text="Name", bg="black", fg="white", font=("Courier", 25)).pack( |
| 18 | + pady=5 |
| 19 | + ) |
| 20 | + name = Text(add_book, height=1, width=30, font=("Courier", 25)) |
| 21 | + name.pack(pady=5) |
| 22 | + Label(add_book, text="Author", bg="black", fg="white", font=("Courier", 25)).pack( |
| 23 | + pady=5 |
| 24 | + ) |
| 25 | + author = Text(add_book, height=1, width=30, font=("Courier", 25)) |
| 26 | + author.pack(pady=5) |
| 27 | + Label(add_book, text="Category", bg="black", fg="white", font=("Courier", 25)).pack( |
| 28 | + pady=5 |
| 29 | + ) |
| 30 | + category = Text(add_book, height=1, width=30, font=("Courier", 25)) |
| 31 | + category.pack(pady=5) |
| 32 | + Label(add_book, text="Count", bg="black", fg="white", font=("Courier", 25)).pack( |
| 33 | + pady=5 |
| 34 | + ) |
| 35 | + count = Text(add_book, height=1, width=30, font=("Courier", 25)) |
| 36 | + count.pack() |
| 37 | + Button( |
| 38 | + add_book, |
| 39 | + text="Submit", |
| 40 | + command=lambda: add_book_query(id, name, category, author, count), |
| 41 | + bg="black", |
| 42 | + fg="white", |
| 43 | + font=("Courier", 25), |
| 44 | + ).pack(pady=12) |
| 45 | + add_book.pack(expand=1, fill=X) |
| 46 | + home.pack_forget() |
| 47 | + |
| 48 | + |
| 49 | +def add_book_query(id, name, category, author, count): |
| 50 | + book_id = id.get(1.0, "end-1c") |
| 51 | + name = name.get(1.0, "end-1c") |
| 52 | + category = category.get(1.0, "end-1c") |
| 53 | + author = author.get(1.0, "end-1c") |
| 54 | + count = count.get(1.0, "end-1c") |
| 55 | + print(book_id, name, category, author, count) |
| 56 | + insert = ( |
| 57 | + "insert into Books values(" |
| 58 | + + book_id |
| 59 | + + ",'" |
| 60 | + + name |
| 61 | + + "','" |
| 62 | + + author |
| 63 | + + "', '" |
| 64 | + + category |
| 65 | + + "'," |
| 66 | + + count |
| 67 | + + "," |
| 68 | + + count |
| 69 | + + ");" |
| 70 | + ) |
| 71 | + |
| 72 | + try: |
| 73 | + connect(insert) |
| 74 | + messagebox.showinfo("Success", "Book added successfully") |
| 75 | + except (pymysql.Error, pymysql.Warning) as e: |
| 76 | + err = str(e) |
| 77 | + messagebox.showinfo("Error", "Can't add data into Database " + err) |
0 commit comments