SQL Database Introduction and base files for coding
SQL Database Introduction
Database
- Organized collection of structured information of data, which is stored in a computer system.
- The data can be easily accessed
- The data can be modified
- The data can be controlled and organized
- Many databases use a structured query language(SQL) to modify and write data
- Data can be related to just about any object.
- For example, a user on an application may have:
- Name
- Age
- Password
- A database is a collection of data
- Since data, on its own, is just data. A database allows management of this data
- Databases are organized in how data can be retrieved, stored and modified
- There are many types of Database Management Systems (DBMS)
- Some of the databases are:
- SQLite
- MySQL
- Postgres
- There are many more.
SQL
- Pronounced either as S-Q-L or "See Quel"
- Standard language for dealing with relational databases
- SQL can be used to do different things with the database records: (CRUD)
- Create
- Read
- Update
- Delete
Create and Install Database File
We need to download SQLALCHEMY for database connection as it is an ORM which will help us to switch between multiple databases without making too much change to any SQL query.
Code: database.py
Here we have base code for database setup not function but will become once completed.
# This file is to make the connection with database for FastAPI framework project.
from sqlalchemy import create_engine
# It will help to create local sessions for each thread to interact with database seperately.
from sqlalchemy.orm import sessionmaker
# importing declarative to create the object of database to control all database activity.
from sqlalchemy.ext.declarative import declarative_base
# Below command will create database in our project folder.
SQLALCHEMY_DATABASE_URL='sqlite:///./todos.db'
# database engine setup as it will let us know about the database which is going to be used.
engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={'check_same_thread':False})
# check_same_thread:False will ensure that no two process can share
# same connection and instance and we not need to check everytime the same thread.
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# object of database.
Base = declarative_base()
Create Database Models
Models:
It help sqlalchemy to understand what type of database are we connecting and accessing for tables performing all CRUD operations over it.
example of table:
Boolean is represented as 0 for False and 1 for True in a table.
Code: models.py
# This will help sqlalchemy to understand the database table and work on it.
from database import Base
from sqlalchemy import Column, Integer, String, Boolean
class Todos(Base):
__tablename__ = 'todos'
id = Column(Integer, primary_key = True, index = True)
title = Column(String)
description = Column(String)
priority = Column(Integer)
complete = Column(Boolean, default = False)
Create our Todos Database
Here we will create the database for storing data in table.
code: main.py
# This file it to perform all the activity which we required for Todo application.
from fastapi import FastAPI
import models
from database import engine
app = FastAPI()
models.Base.metadata.create_all(bind=engine)
Once we will run this main file then it will create a todo.db file in the folder where main is present and then you need to install sqlite to play with this database.

Comments
Post a Comment