Challenge

Project 1 - English Dictionary App & Library Book Management System

#1

import tkinter as tk
from tkinter import messagebox
import sqlite3

# Function to create the 'books' table in the SQLite database
def create_table():
conn = sqlite3.connect('books.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS books
(id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
author TEXT NOT NULL,
year INTEGER NOT NULL,
isbn TEXT);''')
conn.commit()
conn.close()

# Function to insert a new book into the 'books' table
def add_book():
title = title_entry.get()
author = author_entry.get()
year = year_entry.get()
isbn = isbn_entry.get()

if title == '' or author == '' or year == '':
messagebox.showerror('Error', 'Title, Author, and Year fields are required!')
return

conn = sqlite3.connect('books.db')
c = conn.cursor()
c.execute("INSERT INTO books (title, author, year, isbn) VALUES (?, ?, ?, ?)",
(title, author, year, isbn))
conn.commit()
conn.close()

messagebox.showinfo('Success', 'Book added successfully!')
clear_entries()

# Function to search books based on the search criteria
def search_books():
search_term = search_entry.get()
conn = sqlite3.connect('books.db')
c = conn.cursor()
c.execute("SELECT * FROM books WHERE title LIKE ? OR author LIKE ? OR year LIKE ? OR isbn LIKE ?",
('%' + search_term + '%', '%' + search_term + '%', '%' + search_term + '%', '%' + search_term + '%'))
rows = c.fetchall()
conn.close()

results_box.delete(1.0, tk.END)
if not rows:
results_box.insert(tk.END, 'No results found.')
else:
for row in rows:
results_box.insert(tk.END, f'Title: {row[1]}\nAuthor: {row[2]}\nYear: {row[3]}\nISBN: {row[4]}\n\n')

# Function to update the selected book's information
def update_book():
selected_index = results_box.curselection()
if not selected_index:
messagebox.showerror('Error', 'No book selected!')
return

selected_book = results_box.get(selected_index[0])
book_id = selected_book.split('\n')[0].split(': ')[1]

title = title_entry.get()
author = author_entry.get()
year = year_entry.get()
isbn = isbn_entry.get()

if title == '' or author == '' or year == '':
messagebox.showerror('Error', 'Title, Author, and Year fields are required!')
return

conn = sqlite3.connect('books.db')
c = conn.cursor()
c.execute("UPDATE books SET title = ?, author = ?, year = ?, isbn = ? WHERE id = ?",
(title, author, year, isbn, book_id))
conn.commit()
conn.close()

messagebox.showinfo('Success', 'Book updated successfully!')
clear_entries()

# Function to delete the selected book from the database
def delete_book():
selected_index = results_box.curselection()
if not selected_index:
messagebox.showerror('Error', 'No book selected!')
return

selected_book = results_box.get(selected_index[0])
book_id = selected_book.split('\n')[0].split(': ')[1]

conn = sqlite3.connect('books.db)

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.