CSE 4508 RDBMS Lab Task Winter 2024
CSE 4508 RDBMS Lab Task Winter 2024
CSE 4508
RELATIONAL DATABASE MANAGEMENT SYSTEM LAB
NOVEMBER 5, 2024
Winter 2024 Complex Data Types Lab 5
Contents
1 JSON 2
1.1 Understanding JavaScript Object Notation (JSON) . . . . . . . . . . . . . . . . . 2
1.2 Working with JSON in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Accessing Nested JSON Data in Python . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Accessing Arrays in JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 SQLAlchemy 3
2.1 Setting up a Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 Defining Models (Tables) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.3 Creating a Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.4 CRUD Operations with SQLAlchemy . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.4.1 Create: Adding a New User to the Database . . . . . . . . . . . . . . . . 4
2.4.2 Read: Querying Users from the Database . . . . . . . . . . . . . . . . . . 4
2.4.3 Update: Updating an Existing User’s Age . . . . . . . . . . . . . . . . . . 4
2.4.4 Delete: Deleting a User from the Database . . . . . . . . . . . . . . . . . 4
2.5 Working with Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3 Lab Tasks 5
1
Winter 2024 Complex Data Types Lab 5
1 JSON
1.1 Understanding JavaScript Object Notation (JSON)
• Lightweight data-interchange format, commonly used for APIs.
• JSON syntax:
– Key-value pairs
– Arrays and nested objects
2
Winter 2024 Complex Data Types Lab 5
2 SQLAlchemy
SQLAlchemy is a powerful SQL toolkit and Object-Relational Mapping (ORM) library for
Python. It provides both Core (for SQL expressions) and ORM (for mapping tables to
classes). It is used to interact with relational databases (SQLite, PostgreSQL, MySQL).
import os
from sqlalchemy import create_engine , Column , Integer , String ,
ForeignKey
from sqlalchemy . ext . declarative import declarative_base
from sqlalchemy . orm import sessionmaker , relationship
# Connecting to an SQLite database ( or creating it if it doesn ’ t
exist )
current_direc tory = os . path . dirname ( os . path . abspath ( __file__ ) )
database_path = os . path . join ( current_directory , ’ example . db ’)
engine = create_engine (f ’ sqlite :///{ database_path } ’)
3
Winter 2024 Complex Data Types Lab 5
# Fetch the first user with the name " Alice "
user = session . query ( User ) . filter_by ( name =" Alice ") . first ()
print ( user . name , user . age ) # Output : Alice 25
user = session . query ( User ) . filter_by ( name =" Alice ") . first ()
user . age = 26 # Update the age
session . commit () # Save the changes
user = session . query ( User ) . filter_by ( name =" Alice ") . first ()
session . delete ( user ) # Mark the user for deletion
session . commit () # Commit the deletion
4
Winter 2024 Complex Data Types Lab 5
3 Lab Tasks
1. Parse the JSON data and print the store’s "name" and its "city" location.
2. Access and print the names and prices of all products in the "Electronics" department.
3. Extract and print the "brand" and "specifications" of the product with the name "Wire-
less Mouse".
4. Modify the "price" of the "Smartphone" product to 749.99 and print the updated JSON.
1. Define the Book Model: Define a SQLAlchemy model for the Book table with the
specified columns and data types, ensuring the id column is set as the primary key.
2. Create the Book Table: After defining the model, use Base.metadata.create_all(engine)
to create the Book table in the SQLite database.
3. Add a New Book Entry: Use a session to add a new Book record with the following
details, then commit the transaction:
• Title: "The Great Gatsby"
• Author: "F. Scott Fitzgerald"
• Published Year: 1925
• Price: 10.99
4. Read and Display All Books: Query the Book table to retrieve all entries and print
each book’s title, author, published_year, and price.
5. Update the Price of a Book: Find the book with the title "The Great Gatsby"
and update its price to 12.99, then commit the change.
6. Delete a Book Entry: Delete the book entry with the title "The Great Gatsby"
from the database and commit the deletion.
5
Winter 2024 Complex Data Types Lab 5
Submission Guidelines
You are required to submit a report that includes your code, a detailed explanation, and the
corresponding output. Rename your report as <StudentID_Lab_1.pdf>.
• Your lab report must be generated in LaTeX. Recommended tool: Overleaf. You can
use some of the available templates in Overleaf.
• Include all code/pseudo code relevant to the lab tasks in the lab report.
• Please provide citations for any claims that are not self-explanatory or commonly
understood.
• It is recommended to use vector graphics (e.g., .pdf, .svg) for all visualizations.
• In cases of high similarities between two reports, both reports will be discarded.