├── README.md ├── __pycache__ ├── database.cpython-310.pyc ├── databse.cpython-310.pyc ├── main.cpython-310.pyc ├── models.cpython-310.pyc └── schemas.cpython-310.pyc ├── codesnippets ├── basic App.png ├── database.png ├── main.png ├── models.png └── schemas.png ├── database.py ├── main.py ├── models.py ├── requirements.txt ├── schemas.py ├── screenshots └── fastapidocs.jpg └── users.db /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Database handling with FastAPI and SQL 3 | 4 | This simple project is to demonstrate how to use SQL databases with FastAPI 5 | 6 | 7 | ## Run Locally 8 | 9 | Clone the project 10 | 11 | ```bash 12 | git clone https://github.com/yasanthaniroshan/gfg-FastAPI_SQL_Databases.git 13 | ``` 14 | 15 | 16 | Install dependencies 17 | 18 | ```bash 19 | pip install -r requirements.txt 20 | ``` 21 | 22 | Start the server 23 | 24 | ```bash 25 | uvicorn main:app --reload 26 | ``` 27 | 28 | 29 | ## Screenshots 30 | 31 | ![App Screenshot](https://github.com/yasanthaniroshan/gfg-FastAPI_SQL_Databases/blob/main/screenshots/fastapidocs.jpg) 32 | 33 | -------------------------------------------------------------------------------- /__pycache__/database.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yasanthaniroshan/FastAPI_SQL_Databases/36de657bf1dda41632b7391b089a3830ffa28d8f/__pycache__/database.cpython-310.pyc -------------------------------------------------------------------------------- /__pycache__/databse.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yasanthaniroshan/FastAPI_SQL_Databases/36de657bf1dda41632b7391b089a3830ffa28d8f/__pycache__/databse.cpython-310.pyc -------------------------------------------------------------------------------- /__pycache__/main.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yasanthaniroshan/FastAPI_SQL_Databases/36de657bf1dda41632b7391b089a3830ffa28d8f/__pycache__/main.cpython-310.pyc -------------------------------------------------------------------------------- /__pycache__/models.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yasanthaniroshan/FastAPI_SQL_Databases/36de657bf1dda41632b7391b089a3830ffa28d8f/__pycache__/models.cpython-310.pyc -------------------------------------------------------------------------------- /__pycache__/schemas.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yasanthaniroshan/FastAPI_SQL_Databases/36de657bf1dda41632b7391b089a3830ffa28d8f/__pycache__/schemas.cpython-310.pyc -------------------------------------------------------------------------------- /codesnippets/basic App.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yasanthaniroshan/FastAPI_SQL_Databases/36de657bf1dda41632b7391b089a3830ffa28d8f/codesnippets/basic App.png -------------------------------------------------------------------------------- /codesnippets/database.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yasanthaniroshan/FastAPI_SQL_Databases/36de657bf1dda41632b7391b089a3830ffa28d8f/codesnippets/database.png -------------------------------------------------------------------------------- /codesnippets/main.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yasanthaniroshan/FastAPI_SQL_Databases/36de657bf1dda41632b7391b089a3830ffa28d8f/codesnippets/main.png -------------------------------------------------------------------------------- /codesnippets/models.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yasanthaniroshan/FastAPI_SQL_Databases/36de657bf1dda41632b7391b089a3830ffa28d8f/codesnippets/models.png -------------------------------------------------------------------------------- /codesnippets/schemas.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yasanthaniroshan/FastAPI_SQL_Databases/36de657bf1dda41632b7391b089a3830ffa28d8f/codesnippets/schemas.png -------------------------------------------------------------------------------- /database.py: -------------------------------------------------------------------------------- 1 | from sqlalchemy import create_engine 2 | from sqlalchemy.ext.declarative import declarative_base 3 | from sqlalchemy.orm import sessionmaker 4 | 5 | 6 | DATABASE_URL = "sqlite:///./users.db" 7 | engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False}) 8 | SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) 9 | Base = declarative_base() 10 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from fastapi import FastAPI, Depends, HTTPException 2 | from models import Base, User 3 | from schemas import UserSchema 4 | from database import engine,SessionLocal 5 | from sqlalchemy.orm import Session 6 | 7 | Base.metadata.create_all(bind=engine) 8 | 9 | app = FastAPI() 10 | 11 | 12 | def get_db(): 13 | try: 14 | db = SessionLocal() 15 | yield db 16 | finally: 17 | db.close() 18 | 19 | @app.get("/") 20 | async def home(): 21 | return {"message": "Hello, World!"} 22 | 23 | @app.post("/adduser") 24 | async def add_user(request:UserSchema, db: Session = Depends(get_db)): 25 | user = User(name=request.name, email=request.email, nickname=request.nickname) 26 | db.add(user) 27 | db.commit() 28 | db.refresh(user) 29 | return user 30 | 31 | @app.get("/user/{user_name}") 32 | async def get_users(user_name,db: Session = Depends(get_db)): 33 | users = db.query(User).filter(User.name == user_name).first() 34 | return users -------------------------------------------------------------------------------- /models.py: -------------------------------------------------------------------------------- 1 | from sqlalchemy import Column, Integer, String 2 | from database import Base 3 | 4 | class User(Base): 5 | __tablename__ = "users" 6 | 7 | id = Column(Integer, primary_key=True, index=True) 8 | name = Column(String) 9 | email = Column(String) 10 | nickname = Column(String) -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | aiosqlite==0.19.0 2 | annotated-types==0.5.0 3 | anyio==3.7.1 4 | certifi==2023.7.22 5 | click==8.1.7 6 | databases==0.8.0 7 | dnspython==2.4.2 8 | email-validator==2.0.0.post2 9 | exceptiongroup==1.1.3 10 | fastapi==0.103.1 11 | greenlet==2.0.2 12 | h11==0.14.0 13 | httpcore==0.18.0 14 | httptools==0.6.0 15 | httpx==0.25.0 16 | idna==3.4 17 | itsdangerous==2.1.2 18 | Jinja2==3.1.2 19 | MarkupSafe==2.1.3 20 | orjson==3.9.7 21 | pydantic==2.3.0 22 | pydantic-extra-types==2.1.0 23 | pydantic-settings==2.0.3 24 | pydantic_core==2.6.3 25 | python-dotenv==1.0.0 26 | python-multipart==0.0.6 27 | PyYAML==6.0.1 28 | sniffio==1.3.0 29 | SQLAlchemy==1.4.49 30 | starlette==0.27.0 31 | typing_extensions==4.7.1 32 | ujson==5.8.0 33 | uvicorn==0.23.2 34 | uvloop==0.17.0 35 | watchfiles==0.20.0 36 | websockets==11.0.3 37 | -------------------------------------------------------------------------------- /schemas.py: -------------------------------------------------------------------------------- 1 | from pydantic import BaseModel 2 | 3 | class UserSchema(BaseModel): 4 | id: int 5 | name: str 6 | email: str 7 | nickname: str 8 | 9 | -------------------------------------------------------------------------------- /screenshots/fastapidocs.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yasanthaniroshan/FastAPI_SQL_Databases/36de657bf1dda41632b7391b089a3830ffa28d8f/screenshots/fastapidocs.jpg -------------------------------------------------------------------------------- /users.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yasanthaniroshan/FastAPI_SQL_Databases/36de657bf1dda41632b7391b089a3830ffa28d8f/users.db --------------------------------------------------------------------------------