SqlAlchemy with python
By: Sainadh Cherukumalli
Tags: Python, Backend, Sql, Data, Database

In the last blog we discussed about databases and getting started with SqlAlchemy, connecting to database and we also created a table student in the database. click here if you missed it.

In this blog we enter data to database and query on the data.

The simple steps to enter data to any database is to connect to the database.

Then follow the schema of the table to enter data.

In SqlAlchemy we have declarative_base function to write the schema of the table. we inherit the properties of the table. Used for mapping of the database.

from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

From the above base we create a class.

from sqlalchemy import Column, Integer, String,Sequence

We have to import functions for creating the schema of the table.

Column is a function having parameters, Column data type, key type and sequences. Integer and String are the data types.The Sequence function represents the name and configurational parameters of a database sequence.

class Student(Base):

__tablename__ = ‘students’

name = Column(String(50))

reg_no = Column(Integer,Sequence(‘student_reg_no_seq’),primary_key = True)

group_name = Column(String(50))

def __repr__(self):

return “<Student(name=’%s’, reg_no=’%s’)>” %(self.name,self.reg_no)

__repr__ function is used to display the table content. It is optional as we are getting started it is good to write.

print(Student.__table__) #prints the table name.

Entering few data.

std1 = Student(name=’Sai’, reg_no=’123456', group_name=’CSE’)

std2 = Student(name=’Yogesh’,reg_no=’123457', group_name=’EEE’)

std3 = Student(name=’Balaji’, reg_no=’123458', group_name=’IT’)

But this data is not added to the database as we not made any connection to it, we are storing the values in std1,std2 and std3.

We connect to the database

from sqlalchemy import create_engine

engine = create_engine(‘sqlite:////home/varshan/Documents/auto_ml/db.sqlite’, echo=True)

For entering data to the database we have to make a session so that the data will be updated asynchronously. You should also know a concept ORM.

Object-relational mapping(ORM) in computer science is a programming technique for converting data between incompatible type systems using object-oriented programming languages. sessionmaker is the function of ORM.

from sqlalchemy.orm import sessionmaker

Session_maker = sessionmaker(bind=engine)

Session = Session_maker()

The session is started.

And we add the data to the database

Session.add(std1)

Adds a single to row to the database

To add multiple rows Session has add_all function

Session.add_all([std2,std3])

Querying on the data

Session.query(Student).filter_by(group_name=’CSE’)

We will discuss more about the query and data manipulation in next blog.

To update any value in a particular like changing the name

std3.name = ‘Mastan’

Session has a feature dirty which will notify any updated values.

Session.dirty

To check the newly added rows to the table we have ‘new’ feature in the session.

Session.new

To save the changes made to the table we have a function called commit.

Session.commit()

In this blog we discussed about connecting to a database, entering our data to the table, querying on the table(basic) and committing changes to the table.

To get the code check below

Comment below if you have any queries.