ARTICLE AD BOX
I'm working on creating a kind of online library. I'm using SQLite3 with Python, and basically I can add, delete, view my library, and search for books.
But if I select the search option, I can search for a book and it gives me the result, but it keeps asking me which book I’m looking for without returning to the initial prompt, which I don’t understand even after adding a break.
And this isn’t really related to “troubleshooting/debugging,” but for the search function, I’d like to allow users to search for a book or an author along with the books they’ve written. However, for the author search, I’m not sure how to go about it—do you have any ideas?
import sqlite3 import os dossier_actuel = os.path.dirname(os.path.abspath(__file__)) chemin_base_donnees = os.path.join(dossier_actuel, "bibliotheque.db") def connection(): return sqlite3.connect(chemin_base_donnees) def crea_biblio(): try: with connection() as conn: cursor = conn.cursor() sql = '''CREATE TABLE IF NOT EXISTS Livres ( id_livre INTEGER PRIMARY KEY AUTOINCREMENT, titre_livre TEXT, auteur_livre TEXT );''' cursor.execute(sql) conn.commit() except Exception as e: print(f"An error occured while creating the table : {e}") def ajout_livre(): try: with connection() as conn: cursor = conn.cursor() title = input("Which is the tittle of your new book : ") author = input("Who is the author : ") sql = "INSERT INTO Livres (titre_livre, auteur_livre) VALUES (?, ?);" cursor.execute(sql, (title, author)) except Exception as e: print(f"An error occurred while inserting the book: {e}") def supp_livre(): try: with connection() as conn: cursor = conn.cursor() nom = input("What is the title of the book you want to delete : ") sql = '''DELETE FROM Livres WHERE titre_livre = ?;''' cursor.execute(sql, (nom, )) conn.commit() except Exception as e: print(f'An error occured while creating the table : {e}') def view(): try: with connection() as conn: cursor = conn.cursor() cursor.execute("SELECT id_livre, titre_livre, auteur_livre FROM Livres ORDER BY id_livre") lignes = cursor.fetchall() for ligne in lignes: print(ligne[0], ligne[1], ligne[2]) except Exception as e: print(f'An error occured while creating the table : {e}') def search(): try: with connection() as conn: cursor = conn.cursor() while True: recherche = input("Quel livre recherchez-vous : ") if recherche.upper() == 'FIN' : break sql = "select id_livre, titre_livre, auteur_livre from Livres where titre_livre like ?" cursor.execute(sql, (recherche+'%',)) lignes = cursor.fetchall() for ligne in lignes: print(ligne[1], ligne[2]) break except Exception as e: print(f'An error occured while creating the table : {e}') while True: connection() crea_biblio() choice = input("Welcome to your online library. Would you like to (1) add a new book, (2) delete a book, (3) view your library, or (4) search for a book? ") if choice=="1": ajout_livre() elif choice=="2": supp_livre() elif choice=="3": view() elif choice=="4": search() elif choice=="q": breakP.S : Sorry, some parts are in French and others in English—I didn't take the time to standardize everything.
