python_by_example/sqlite/sql05.py
2023-11-20 01:20:52 -03:00

19 lines
1.0 KiB
Python

from os import getcwd as pwd
import sqlite3
def sql_05():
"""Using the BookInfo database, ask the user to enter a year and display all
the books published after that year, sorted by the year they were published."""
db_path = f"{pwd()}/sqlite/db/BookInfo.db"
with sqlite3.connect(db_path) as db:
cursor = db.cursor()
query = "SELECT * FROM libros WHERE fecha>=? ORDER BY fecha"
sel = input("\nIngresa un año de publicación: ")
cursor.execute(query, [sel])
print("\n - Titulo ", end='')
print(" Autor Publicado")
print("━━━ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━", end='')
print(" ━━━━━━━━━━━━━━━ ━━━━━━━━━━━")
for libro in cursor.fetchall():
print(str(libro[0]).ljust(3), libro[1].ljust(45), libro[2].ljust(15), libro[3])