This academic Jupyter Notebook demonstrates foundational database programming with Python. It uses the standard-library sqlite3 module and raw SQL to create and populate a file-backed SQLite database, then uses SQLAlchemy Core to reflect the existing table and query its records.
The notebook also contains two small Python module-import exercises before the database section. These exercises reference a local zoo module and are separate from the database workflow.
- Connect to a file-backed SQLite database
- Create a database table with raw SQL
- Insert records with SQL statements
- Commit database transactions
- Close a direct SQLite connection
- Connect to SQLite through SQLAlchemy
- Reflect an existing table with SQLAlchemy Core
- Select and sort records
- Fetch and iterate over query results
The first two exercises demonstrate importing a local Python module:
- Importing
zoodirectly - Importing
zoowith the aliasmenagerie - Calling the module’s
hours()function
The required zoo.py file is not currently included in this repository. These cells are unrelated to the database exercise and cannot run from a clean clone without that module.
The database section performs the following sequence:
- Installs SQLAlchemy from inside the notebook.
- Imports Python’s built-in
sqlite3module. - Opens or creates a file-backed SQLite database named
books.db. - Creates a
booktable using raw SQL. - Inserts four book titles using raw
INSERTstatements. - Commits the database changes.
- Closes the direct SQLite connection.
- Creates a SQLAlchemy engine connected to the same database file.
- Reflects the existing
booktable with SQLAlchemy Core. - Selects the
titlecolumn in alphabetical order. - Fetches and prints the query results.
The notebook creates one table:
| Field | Type | Constraint |
|---|---|---|
id |
INTEGER |
PRIMARY KEY |
title |
TEXT |
None explicitly defined |
No additional constraints, relationships, or indexes are defined.
| Operation | Implemented? | Method |
|---|---|---|
| Connect to SQLite | Yes | sqlite3.connect('books.db') |
| Create a table | Yes | Raw SQL CREATE TABLE |
| Insert records | Yes | Raw SQL INSERT INTO |
| Select/read records | Yes | SQLAlchemy Core select() |
| Order query results | Yes | SQLAlchemy Core order_by() |
| Fetch query results | Yes | fetchall() |
| Commit changes | Yes | conn.commit() |
| Close direct SQLite connection | Yes | conn.close() |
| Update records | No | Not implemented |
| Delete records | No | Not implemented |
| Define ORM models | No | SQLAlchemy Core table reflection is used |
The notebook demonstrates create, insert, and read operations. It is not a complete CRUD implementation.
- Python
- Jupyter Notebook
- Python
sqlite3 - SQLite
- SQL
- SQLAlchemy Core
The committed notebook displays the book titles in alphabetical order:
1984
Animal Farm
Brave New World
The Alchemist
You will need:
- Python 3
- Jupyter Notebook
- SQLAlchemy
Install the required tools manually:
python -m pip install jupyter SQLAlchemygit clone https://github.com/asmaayasser1/Module-4-and-Databases.git
cd Module-4-and-Databasesjupyter notebookOpen:
Module 4 and Databases.ipynb
The database section can be run independently after SQLAlchemy is installed. The earlier module-import exercises require zoo.py, which is not included in the repository.
Running the database creation cell generates:
books.db
This is a file-backed SQLite database created in the notebook’s working directory. The database file is not committed to this repository.
The repository is not fully reproducible from a clean clone without additional context:
zoo.pyis required by the first two code cells but is not included.- SQLAlchemy must be installed before the SQLAlchemy query can run.
- The database creation cell must run before the SQLAlchemy query cell.
- Table creation is not idempotent.
- Rerunning the creation cell after
books.dband thebooktable already exist may produce a table-exists error. - The saved execution counts are
2,3,5,7, and8, indicating that the notebook was not saved after a clean sequential top-to-bottom execution.
These conditions describe the repository’s current state; the notebook code and outputs have not been changed.
- No update operation is implemented.
- No delete operation is implemented.
- SQLAlchemy ORM models are not used.
- No database migration workflow is included.
- No automated tests are provided.
- The SQLAlchemy connection is not explicitly closed.
- The notebook installs SQLAlchemy during execution rather than using a dependency manifest.
- The stored installation output is environment-specific.
- Unrelated module-import exercises are mixed with the database exercise.
- The required
zoo.pymodule is absent. - Repeated execution is not safely handled when the database table already exists.
This repository is an academic exercise focused on foundational Python module usage and database programming. It demonstrates direct SQLite access, basic SQL statements, and SQLAlchemy Core querying without presenting itself as a complete database application.
Academic Python database project being refined for professional portfolio presentation.