0% found this document useful (0 votes)
168 views3 pages

Sqlalchemy: From Import

This document demonstrates how to perform CRUD (create, read, update, delete) operations using SQLAlchemy with both a traditional SQL database and ORM (object relational mapping). It shows how to create a table and insert records, read the records, update a record, and delete a record. It then demonstrates the same operations using SQLAlchemy's ORM features to map Python classes to database tables.

Uploaded by

Girish Sawant
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)
168 views3 pages

Sqlalchemy: From Import

This document demonstrates how to perform CRUD (create, read, update, delete) operations using SQLAlchemy with both a traditional SQL database and ORM (object relational mapping). It shows how to create a table and insert records, read the records, update a record, and delete a record. It then demonstrates the same operations using SQLAlchemy's ORM features to map Python classes to database tables.

Uploaded by

Girish Sawant
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
You are on page 1/ 3

SQLALCHEMY

from sqlalchemy import create_engine

db_string = "sqlite:///tests.db"

db = create_engine(db_string)

#create
db.execute("CREATE TABLE IF NOT EXISTS players (plyid text, plyname text,runs 
text")

db.execute("INSERT INTO players(plyis,plyname,runs) VALUES ('10001', 'ply1','1
00'),('10002' , 'ply2', '80'),('10003','ply3','65'),('10004','ply4','95'),
('10005','ply5','99')")

# Read
result_set =db.execute("SELECT * FROM players")
s=[]
for r in result_set:
  s.append(r)

#Update
mal = db.execute("UPDATE players  SET runs='100' WHERE plyname='ply5'")
result_set1 =db.execute("SELECT * FROM players")
q=[]
for w in result_set1:
  q.append(w)

#Delete

db.execute("DELETE FROM players WHERE plyname ='ply5'")

result_set2 =db.execute("SELECT * FROM players")

e=[]
for h in result_set2:
  e.append(h)

ORM

from sqlalchemy import create_engine  
from sqlalchemy import Column, String  
from sqlalchemy.ext.declarative import declarative_base  
from sqlalchemy.orm import sessionmaker

db_string = "sqlite:///tests.db"
db = create_engine(db_string)  
base = declarative_base()

class Teacher(base):

  __tablename__ = 'students'  
  stdid = Column(String, primary_key =True)
  stdname = Column(String)
  subjects = Column(String)
  marks = Column(String)

Session = sessionmaker(db)  
session = Session()

base.metadata.create_all(db)

#Create

stgd1 = Teacher(stdid="10001", stdname="std1", subjects="Maths", marks="100")
stgd2 = Teacher(stdid="10002", stdname="std2", subjects="Physics", marks="80")
stgd3 = Teacher(stdid="10003", stdname="std3", subjects="English", marks="65")
stgd4 = Teacher(stdid="10004", stdname="std4", subjects="Social", marks="95")
stgd5 = Teacher(stdid="10005", stdname="std5", subjects="Chemistry", marks="99
")

session.add_all([stgd1,stgd2,stgd3,stgd4,stgd5])
session.commit()
#Read
students = session.query(Teacher)

s = []
for u in students:
  k = [u.stdid, u.stdname, u.subjects, u.marks]
  s.append(k)
print(s)

#Update
stgd5.subjects = "Language"
session.commit()

students = session.query(Teacher)

for n in students:
  h = [n.stdid, n.stdname, n.subjects, n.marks]
  q =list(h)
print(q)

#Delete
session.delete(stgd5)
students =session.query(Teacher)
e =[]
for c in students:
  r = [c.stdid, c.stdname, c.subjects, c.marks]
  e.append(r)
print(r)

You might also like