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

25 lines
1.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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))