python_by_example/sqlite/sql04.py

25 lines
1.5 KiB
Python
Raw Normal View History

2023-11-20 01:20:52 -03:00
from os import getcwd as pwd
import sqlite3
def sql_04():
"""Using the BookInfo database from program 141, display the list of authors
and their place of birth. Ask the user to enter a place of birth and then
show the title, date published and authors name for all the books by
authors who were born in the location they selected."""
db_path = f"{pwd()}/sqlite/db/BookInfo.db"
with sqlite3.connect(db_path) as db:
cursor = db.cursor()
cursor.execute("SELECT * FROM autores")
print("\n - Autor Nacimiento")
print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
for autor in cursor.fetchall():
print(str(autor[0]).ljust(4), autor[1].ljust(20), autor[2].ljust(15))
sel = input("\nIngresa un lugar de nacimiento: ")
query = """SELECT titulo, fecha, nombre FROM libros, autores
WHERE autores.nombre=libros.autor AND autores.nacimiento=?"""
cursor.execute(query, [sel])
print("\n Titulo Publicado Autor")
print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
for result in cursor.fetchall():
print(result[0].ljust(45), result[1].ljust(10), result[2].ljust(20))