25 lines
1.1 KiB
Python
25 lines
1.1 KiB
Python
from os import getcwd as pwd
|
|
import sqlite3
|
|
|
|
def sql_06():
|
|
"""Using the BookInfo database, ask the user for an author's name and then
|
|
save all the books by that author to a text file, with each field
|
|
separated by dashes so it looks as follows:
|
|
5 - Murder on the Orient Express - Agatha Cristie - 1934
|
|
8 - The murder on the links - Agatha Cristie - 1923
|
|
....
|
|
Open the text file to make sure it has worked correctly."""
|
|
db_path = f"{pwd()}/sqlite/db/BookInfo.db"
|
|
file_path = f"{pwd()}/sqlite/db/file.txt"
|
|
result = []
|
|
with sqlite3.connect(db_path) as db:
|
|
cursor = db.cursor()
|
|
autor = input("\nIngresa el nombre del autor del libro a buscar: ")
|
|
query = f"SELECT * FROM libros WHERE autor LIKE '%{autor}%'"
|
|
cursor.execute(query)
|
|
for libro in cursor.fetchall():
|
|
result.append(str(libro[0])+" - "+libro[1]+" - "+libro[2]+" - "+libro[3]+"\n")
|
|
with open(file_path, 'w') as file:
|
|
file.writelines(result)
|
|
print(f"Ruta archivo: {file_path}")
|