Avatar
Fernando Vásquez is an electronics engineer and software developer currently based in the world. He occasionally blogs about Python and Android programming.

SQLAlchemy


layout: post title: “SQLAlchemy” description: this article explains something and gives a brief introduction. tags: welcome introduction —

db.Column(db.BigInteger)
Boolean

Column(DateTime, default=datetime.datetime.utcnow)

  • Full cheat sheet https://www.pythonsheets.com/notes/python-sqlalchemy.html
  • Table relation https://hackersandslackers.com/sqlalchemy-data-models/
  • CRUD https://medium.com/@allwindicaprio/crud-operations-using-flask-and-sqlalchemy-7291e340dcc8
book = Book(name="The monk who", author="Robin Sharma", published="1997-03-04")
session.add(book)
session.commit()

session.delete(book)
session.commit()

Quering

book = Book.query.all()
book = Book.query.get(1)

session.query(User).filter(User.age == 25)
query = session.query(User).order_by(User.id)
query = session.query(User).order_by(desc(User.id))
query = session.query(User).order_by(User.birth)
query = session.query(User).filter(User.id==2)
_row = query.first()
query = session.query(User).filter(User.id!=2)
query = session.query(User).filter(User.name.in_(['ed', 'wendy']))
query = session.query(User).filter(~User.name.in_(['ed', 'wendy']))
query = session.query(User).filter(User.name=='ed', User.fullname=='Ed Jones')
_row = query.first()

query = session.query(User).filter(or_(User.name=='ed', User.name=='wendy'))
query = session.query(User).filter(User.birth == None)
query = session.query(User).filter(User.birth != None)
query = session.query(User).filter(User.name.like('%ed%'))

all tags