Applying Filter
In this chapter, we will discuss how to apply filter and also certain filter operations along with their codes. Resultset represented by Query object can be subjected to certain criteria by using filter() method. The general usage of filter method is as follows:
- session.query(class).filter(criteria)
- result = session.query(Customers).filter(Customers.id>2)
- SELECT customers.id
- AS customers_id, customers.name
- AS customers_name, customers.address
- AS customers_address, customers.email
- AS customers_email
- FROM customers
- WHERE customers.id > ?
- demo23.py
- from sqlalchemy import Column, Integer, String
- from sqlalchemy import create_engine
- from sqlalchemy.ext.declarative import declarative_base
- from sqlalchemy.orm.session import sessionmaker
- db_string = "postgresql://postgres:[password]@localhost/testdb"
- engine = create_engine(db_string, echo=True)
- Base = declarative_base()
- class Customers(Base):
- __tablename__ = 'customers'
- id = Column(Integer, primary_key=True)
- name = Column(String)
- address = Column(String)
- email = Column(String)
- Base.metadata.create_all(engine)
- Session = sessionmaker(bind = engine)
- session = Session()
- result = session.query(Customers).all()
- result = session.query(Customers).filter(Customers.id>2)
- for row in result:
- print("ID: {}; Name: {}; Address: {}; Email: {}".format(row.id, row.name, row.address, row.email))
Filter Operators
Now, we will learn the filter operations with their respective codes and output.
Equals
The usual operator used is == and it applies the criteria to check equality.
- result = session.query(Customers).filter(Customers.id == 2)
- for row in result:
- print ("ID:", row.id, "Name: ",row.name, "Address:",row.address, "Email:",row.email)
- SELECT customers.id
- AS customers_id, customers.name
- AS customers_name, customers.address
- AS customers_address, customers.email
- AS customers_email
- FROM customers
- WHERE customers.id = ?
Not Equals
The operator used for not equals is != and it provides not equals criteria:
- result = session.query(Customers).filter(Customers.id! = 2)
- for row in result:
- print ("ID:", row.id, "Name: ",row.name, "Address:",row.address, "Email:",row.email)
- SELECT customers.id
- AS customers_id, customers.name
- AS customers_name, customers.address
- AS customers_address, customers.email
- AS customers_email
- FROM customers
- WHERE customers.id != ?
Like
like() method itself produces the LIKE criteria for WHERE clause in the SELECT expression:
- result = session.query(Customers).filter(Customers.name.like('Ra%'))
- for row in result:
- print ("ID:", row.id, "Name: ",row.name, "Address:",row.address, "Email:",row.email)
- SELECT customers.id
- AS customers_id, customers.name
- AS customers_name, customers.address
- AS customers_address, customers.email
- AS customers_email
- FROM customers
- WHERE customers.name LIKE ?
And the output for the above code is:
IN
This operator checks whether the column value belongs to a collection of items in a list. It is provided by in_() method:
- result = session.query(Customers).filter(Customers.id.in_([1,3]))
- for row in result:
- print ("ID:", row.id, "Name: ",row.name, "Address:",row.address, "Email:",row.email)
- SELECT customers.id
- AS customers_id, customers.name
- AS customers_name, customers.address
- AS customers_address, customers.email
- AS customers_email
- FROM customers
- WHERE customers.id IN (?, ?)
AND
This conjunction is generated by either putting multiple commas separated criteria in the filter or using and_() method as given below:
- result = session.query(Customers).filter(Customers.id>2, Customers.name.like('Ra%'))
- for row in result:
- print ("ID:", row.id, "Name: ",row.name, "Address:",row.address, "Email:",row.email)
- from sqlalchemy import and_
- result = session.query(Customers).filter(and_(Customers.id>2, Customers.name.like('Ra%')))
- for row in result:
- print ("ID:", row.id, "Name: ",row.name, "Address:",row.address, "Email:",row.email)
- SELECT customers.id
- AS customers_id, customers.name
- AS customers_name, customers.address
- AS customers_address, customers.email
- AS customers_email
- FROM customers
- WHERE customers.id > ? AND customers.name LIKE ?
OR
This conjunction is implemented by or_() method:
- from sqlalchemy import or_
- result = session.query(Customers).filter(or_(Customers.id>2, Customers.name.like('Ra%')))
- for row in result:
- print ("ID:", row.id, "Name: ",row.name, "Address:",row.address, "Email:",row.email)
- SELECT customers.id
- AS customers_id, customers.name
- AS customers_name, customers.address
- AS customers_address, customers.email
- AS customers_email
- FROM customers
- WHERE customers.id > ? OR customers.name LIKE ?
The output for the above code is as follows:
A full example code to demonstration the usage:
- demo24.py
- from sqlalchemy import Column, Integer, String
- from sqlalchemy.sql.expression import or_
- from sqlalchemy import create_engine
- from sqlalchemy.ext.declarative import declarative_base
- from sqlalchemy.orm.session import sessionmaker
- db_string = "postgresql://postgres:[Password]@localhost/testdb"
- engine = create_engine(db_string, echo=True)
- Base = declarative_base()
- class Customers(Base):
- __tablename__ = 'customers'
- id = Column(Integer, primary_key=True)
- name = Column(String)
- address = Column(String)
- email = Column(String)
- Base.metadata.create_all(engine)
- Session = sessionmaker(bind = engine)
- session = Session()
- result = session.query(Customers).all()
- result = session.query(Customers).filter(or_(Customers.id!=1, Customers.name.like('Ra%')), Customers.id.in_([2,3]))
- for row in result:
- print("ID: {}; Name: {}; Address: {}; Email: {}".format(row.id, row.name, row.address, row.email))
Supplement
* SQLAlchemy 1.3 Documentation - Query API
* SQLAlchemy 1.3 Documentation - Column Elements and Expressions
沒有留言:
張貼留言