diff --git a/.gitignore b/.gitignore index 1f84a50ea..8d1936259 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ +.venv .env -.learn diff --git a/database.db b/database.db new file mode 100644 index 000000000..79cbca6e4 Binary files /dev/null and b/database.db differ diff --git a/requirements.txt b/requirements.txt index 2f24fd415..5c5089000 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,6 +4,7 @@ pandas python-dotenv==0.20.0 psycopg2-binary==2.9.3 + numpy>=1.18.5 # opencv-python>=4.1.2 # matplotlib>=3.2.2 diff --git a/src/app.py b/src/app.py index 0206b9347..9f17e2985 100644 --- a/src/app.py +++ b/src/app.py @@ -1,79 +1,77 @@ import os import pandas as pd -from sqlalchemy import create_engine, text from dotenv import load_dotenv +import sqlite3 # Load environment variables load_dotenv() -# 1) Connect to the database with SQLAlchemy +# 1) Connect to the database with SQLite def connect(): global engine try: - connection_string = f"postgresql://{os.getenv('DB_USER')}:{os.getenv('DB_PASSWORD')}@{os.getenv('DB_HOST')}/{os.getenv('DB_NAME')}" - print("Starting the connection...") - engine = create_engine(connection_string, isolation_level="AUTOCOMMIT") - engine.connect() - print("Connected successfully!") - return engine + db_name = "database.db" # Nombre de la base de datos SQLite + print("Iniciando conexión...") + conn = sqlite3.connect(db_name) # Crear conexión + print("Conectado exitosamente!") + return conn except Exception as e: - print(f"Error connecting to the database: {e}") + print(f"Error al conectar a la base de datos: {e}") return None -engine = connect() +conn = connect() -if engine is None: +if conn is None: exit() # 2) Create the tables -with engine.connect() as connection: - connection.execute(text(""" +with conn: + conn.executescript(""" CREATE TABLE IF NOT EXISTS publishers ( - publisher_id SERIAL PRIMARY KEY, - name VARCHAR(255) NOT NULL + publisher_id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL ); CREATE TABLE IF NOT EXISTS authors ( - author_id SERIAL PRIMARY KEY, - first_name VARCHAR(100) NOT NULL, - middle_name VARCHAR(50) NULL, - last_name VARCHAR(100) NULL + author_id INTEGER PRIMARY KEY AUTOINCREMENT, + first_name TEXT NOT NULL, + middle_name TEXT NULL, + last_name TEXT NULL ); CREATE TABLE IF NOT EXISTS books ( - book_id SERIAL PRIMARY KEY, - title VARCHAR(255) NOT NULL, - total_pages INT NULL, - rating DECIMAL(4, 2) NULL, - isbn VARCHAR(13) NULL, + book_id INTEGER PRIMARY KEY AUTOINCREMENT, + title TEXT NOT NULL, + total_pages INTEGER NULL, + rating REAL NULL, + isbn TEXT NULL, published_date DATE, - publisher_id INT NULL, - CONSTRAINT fk_publisher FOREIGN KEY (publisher_id) REFERENCES publishers(publisher_id) ON DELETE SET NULL + publisher_id INTEGER NULL, + FOREIGN KEY (publisher_id) REFERENCES publishers(publisher_id) ON DELETE SET NULL ); CREATE TABLE IF NOT EXISTS book_authors ( - book_id INT NOT NULL, - author_id INT NOT NULL, + book_id INTEGER NOT NULL, + author_id INTEGER NOT NULL, PRIMARY KEY (book_id, author_id), - CONSTRAINT fk_book FOREIGN KEY (book_id) REFERENCES books(book_id) ON DELETE CASCADE, - CONSTRAINT fk_author FOREIGN KEY (author_id) REFERENCES authors(author_id) ON DELETE CASCADE + FOREIGN KEY (book_id) REFERENCES books(book_id) ON DELETE CASCADE, + FOREIGN KEY (author_id) REFERENCES authors(author_id) ON DELETE CASCADE ); - """)) + """) # 3) Insert data -with engine.connect() as connection: - connection.execute(text(""" - INSERT INTO publishers (publisher_id, name) VALUES +with conn: + conn.executescript(""" + INSERT OR IGNORE INTO publishers (publisher_id, name) VALUES (1, 'O Reilly Media'), (2, 'A Book Apart'), (3, 'A K PETERS'), (4, 'Academic Press'), (5, 'Addison Wesley'), (6, 'Albert&Sweigart'), - (7, 'Alfred A. Knopf') - ON CONFLICT (publisher_id) DO NOTHING; + (7, 'Alfred A. Knopf'); - INSERT INTO authors (author_id, first_name, middle_name, last_name) VALUES + INSERT OR IGNORE INTO authors (author_id, first_name, middle_name, last_name) VALUES (1, 'Merritt', NULL, 'Eric'), (2, 'Linda', NULL, 'Mui'), (3, 'Alecos', NULL, 'Papadatos'), @@ -81,10 +79,41 @@ def connect(): (5, 'David', NULL, 'Cronin'), (6, 'Richard', NULL, 'Blum'), (7, 'Yuval', 'Noah', 'Harari'), - (8, 'Paul', NULL, 'Albitz') - ON CONFLICT (author_id) DO NOTHING; - """)) + (8, 'Paul', NULL, 'Albitz'); + + INSERT OR IGNORE INTO books (book_id, title, total_pages, rating, isbn, published_date, publisher_id) VALUES + (1, 'Lean Software Development: An Agile Toolkit', 240, 4.17, '9780320000000', '2003-05-18', 5), + (2, 'Facing the Intelligence Explosion', 91, 3.87, NULL, '2013-02-01', 7), + (3, 'Scala in Action', 419, 3.74, '9781940000000', '2013-04-10', 1), + (4, 'Patterns of Software: Tales from the Software Community', 256, 3.84, '9780200000000', '1996-08-15', 1), + (5, 'Anatomy Of LISP', 446, 4.43, '9780070000000', '1978-01-01', 3), + (6, 'Computing machinery and intelligence', 24, 4.17, NULL, '2009-03-22', 4), + (7, 'XML: Visual QuickStart Guide', 269, 3.66, '9780320000000', '2009-01-01', 5), + (8, 'SQL Cookbook', 595, 3.95, '9780600000000', '2005-12-01', 7), + (9, 'The Apollo Guidance Computer: Architecture And Operation (Springer Praxis Books / Space Exploration)', 439, 4.29, '9781440000000', '2010-07-01', 6), + (10, 'Minds and Computers: An Introduction to the Philosophy of Artificial Intelligence', 222, 3.54, '9780750000000', '2007-02-13', 7); + + INSERT OR IGNORE INTO book_authors (book_id, author_id) VALUES + (1, 1), + (2, 8), + (3, 7), + (4, 6), + (5, 5), + (6, 4), + (7, 3), + (8, 2), + (9, 4), + (10, 1); + + + + """) # 4) Use Pandas to read and display a table -df = pd.read_sql("SELECT * FROM publishers;", engine) -print(df) +df_books = pd.read_sql("SELECT * FROM books;", conn) + +print("\nLibros:") +print(df_books) + +# 5) Close connection +conn.close() \ No newline at end of file