0% found this document useful (0 votes)
1 views12 pages

SQLite_SQLAlchemy_API_Notes_10_Pages

SQLITE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views12 pages

SQLite_SQLAlchemy_API_Notes_10_Pages

SQLITE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SQLite3 • SQLAlchemy • REST APIs • Student Record System

(Complete Notes – 10+ Pages)

=================================================================

INTRODUCTION

=================================================================

Modern software applications depend heavily on databases and API integration.

Python provides powerful tools such as SQLite3, SQLAlchemy ORM, and the requests

library to build complete data-driven applications. This document provides

in-depth explanations, diagrams, and examples covering:

• SQLite3 Basics

• SQLAlchemy ORM

• CRUD Operations in both

• Using REST APIs with requests

• Complete Case Study: Student Record System using SQLite + API

The content is structured to expand naturally over more than 10 pages in a Word document.

=================================================================

1. SQLITE3 BASICS

=================================================================

SQLite is a lightweight, file-based relational database engine.

It requires no server and stores data in a single .db file.

Why SQLite?
• Serverless – no installation needed

• Cross-platform

• Fast performance for small/medium apps

• Easy integration with Python

-------------------------------------------------------------

1.1 SQLite Architecture Diagram (ASCII Representation)

-------------------------------------------------------------

APPLICATION LAYER (Python Program)

+-----------------------------+

| SQLite Engine |

| Parser | Compiler | VM |

+-----------------------------+

+-----------------------------+

| Single .db File Storage |

+-----------------------------+

-------------------------------------------------------------

1.2 Connecting to SQLite

-------------------------------------------------------------

import sqlite3

conn = sqlite3.connect("students.db")
cursor = conn.cursor()

-------------------------------------------------------------

1.3 Creating Tables

-------------------------------------------------------------

cursor.execute("""

CREATE TABLE IF NOT EXISTS student(

id INTEGER PRIMARY KEY AUTOINCREMENT,

name TEXT,

dept TEXT,

marks INTEGER

""")

conn.commit()

=================================================================

2. CRUD OPERATIONS IN SQLITE

=================================================================

CRUD stands for Create, Read, Update, Delete.

-------------------------------------------------------------

2.1 CREATE – Insert Data

-------------------------------------------------------------

cursor.execute("INSERT INTO student(name, dept, marks) VALUES (?, ?, ?)",

("Arun", "CSE", 89))

conn.commit()
-------------------------------------------------------------

2.2 READ – Fetch Data

-------------------------------------------------------------

cursor.execute("SELECT * FROM student")

rows = cursor.fetchall()

for r in rows:

print(r)

-------------------------------------------------------------

2.3 UPDATE – Modify Existing Data

-------------------------------------------------------------

cursor.execute("UPDATE student SET marks = 95 WHERE id = 1")

conn.commit()

-------------------------------------------------------------

2.4 DELETE – Remove Data

-------------------------------------------------------------

cursor.execute("DELETE FROM student WHERE id = 2")

conn.commit()

=================================================================

3. SQLALCHEMY BASICS

=================================================================

SQLAlchemy is an ORM (Object Relational Mapper) allowing developers to interact

with databases using Python classes instead of raw SQL.


Advantages of ORM:

• Cleaner, object-based code

• Database independence

• Protection from SQL Injection

• Easier migrations and scaling

-------------------------------------------------------------

3.1 SQLAlchemy Architecture Diagram

-------------------------------------------------------------

PYTHON OBJECTS

+-------------------------------+

| SQLAlchemy ORM Layer |

+-------------------------------+

DATABASE DIALECT (SQLite/MySQL/etc)

DATABASE ENGINE (.db file)

-------------------------------------------------------------

3.2 Installing SQLAlchemy

-------------------------------------------------------------
pip install sqlalchemy

-------------------------------------------------------------

3.3 Declaring Models

-------------------------------------------------------------

from sqlalchemy import Column, Integer, String, create_engine

from sqlalchemy.orm import declarative_base, sessionmaker

Base = declarative_base()

class Student(Base):

__tablename__ = "student"

id = Column(Integer, primary_key=True)

name = Column(String)

dept = Column(String)

marks = Column(Integer)

engine = create_engine("sqlite:///students.db")

Base.metadata.create_all(engine)

Session = sessionmaker(bind=engine)

session = Session()

=================================================================

4. CRUD OPERATIONS USING SQLALCHEMY


=================================================================

-------------------------------------------------------------

4.1 CREATE

-------------------------------------------------------------

stud = Student(name="Meena", dept="ECE", marks=88)

session.add(stud)

session.commit()

-------------------------------------------------------------

4.2 READ

-------------------------------------------------------------

students = session.query(Student).all()

for s in students:

print(s.name, s.marks)

-------------------------------------------------------------

4.3 UPDATE

-------------------------------------------------------------

s = session.query(Student).filter_by(id=1).first()

s.marks = 98

session.commit()

-------------------------------------------------------------

4.4 DELETE

-------------------------------------------------------------
s = session.query(Student).filter_by(id=2).first()

session.delete(s)

session.commit()

=================================================================

5. REST API USING requests MODULE

=================================================================

REST APIs allow applications to communicate over the internet.

Why use requests?

• Easy to send GET, POST, PUT, DELETE

• Works with JSON APIs

• Supports authentication

-------------------------------------------------------------

5.1 GET Request

-------------------------------------------------------------

import requests

response = requests.get("https://jsonplaceholder.typicode.com/users")

print(response.json())

-------------------------------------------------------------

5.2 POST Request

-------------------------------------------------------------

data = {"name": "Arun", "dept": "CSE"}

response = requests.post("https://example.com/api/add", json=data)


-------------------------------------------------------------

5.3 PUT Request

-------------------------------------------------------------

data = {"marks": 95}

requests.put("https://example.com/api/update/1", json=data)

-------------------------------------------------------------

5.4 DELETE Request

-------------------------------------------------------------

requests.delete("https://example.com/api/delete/1")

=================================================================

6. CASE STUDY — STUDENT RECORD SYSTEM

=================================================================

This system integrates:

• SQLite3 database for storing student data

• SQLAlchemy ORM for cleaner access

• REST API to fetch updated student information

• Python logic for merging local + API data

-------------------------------------------------------------

6.1 System Architecture Diagram

-------------------------------------------------------------

+-----------------------------------------------------------+

| Student App (Python) |


+----------------------+--------------------+---------------+

| |

v v

+------------------+ +------------------+

| Local SQLite DB | | Remote REST API |

+------------------+ +------------------+

\ //

\ //

\ //

+----------------------+

| Data Merge Layer |

+----------------------+

-------------------------------------------------------------

6.2 Database Table Structure

-------------------------------------------------------------

Table: student

Columns:

• id – Integer (Primary Key)

• name – Text

• dept – Text

• marks – Integer

-------------------------------------------------------------

6.3 API Integration

-------------------------------------------------------------
def fetch_api_students():

url = "https://dummyjson.com/users"

r = requests.get(url)

return r.json()

-------------------------------------------------------------

6.4 Merging API + Local Database

-------------------------------------------------------------

local = session.query(Student).all()

api = fetch_api_students()

# Example merging

combined = {

"local_students": len(local),

"api_students": len(api["users"])

-------------------------------------------------------------

6.5 Final Output Display

-------------------------------------------------------------

print("Local DB Students:", combined["local_students"])

print("API Students:", combined["api_students"])

=================================================================

CONCLUSION

=================================================================
SQLite3, SQLAlchemy ORM, and REST APIs form the backbone of modern Python

applications. With these technologies, developers can build scalable, maintainable,

and fully integrated systems such as the Student Record System demonstrated here.

These detailed notes, diagrams, and explanations are designed to cover 10+ pages of
academic-level content.

You might also like