SQLAlchemy ORM - फ़िल्टर ऑपरेटर

अब, हम उनके संबंधित कोड और आउटपुट के साथ फ़िल्टर संचालन सीखेंगे।

बराबर

प्रयुक्त सामान्य ऑपरेटर == है और यह समानता की जांच करने के लिए मापदंड लागू करता है।

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)

SQLAlchemy SQL अभिव्यक्ति के बाद भेजेगा -

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 = ?

उपरोक्त कोड के लिए आउटपुट निम्नानुसार है -

ID: 2 Name: Komal Pande Address: Banjara Hills Secunderabad Email: [email protected]

न के बराबर

बराबर नहीं के लिए उपयोग किया जाने वाला ऑपरेटर! = है और यह समान मापदंड प्रदान नहीं करता है।

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)

परिणामी SQL अभिव्यक्ति है -

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 != ?

कोड की उपरोक्त लाइनों के लिए आउटपुट निम्नानुसार है -

ID: 1 Name: Ravi Kumar Address: Station Road Nanded Email: [email protected]
ID: 3 Name: Rajender Nath Address: Sector 40, Gurgaon Email: [email protected]
ID: 4 Name: S.M.Krishna Address: Budhwar Peth, Pune Email: [email protected]

पसंद

जैसे () विधि ही सेलेक्ट एक्सप्रेशन में WHERE क्लॉज के लिए LIKE मापदंड बनाती है।

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)

SQLAlchemy कोड से ऊपर SQL अभिव्यक्ति के बराबर है -

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 ?

और उपरोक्त कोड के लिए आउटपुट है -

ID: 1 Name: Ravi Kumar Address: Station Road Nanded Email: [email protected]
ID: 3 Name: Rajender Nath Address: Sector 40, Gurgaon Email: [email protected]

में

यह ऑपरेटर यह जाँचता है कि सूची में आइटम का संग्रह करने के लिए स्तंभ मान है या नहीं। यह in_ () विधि द्वारा प्रदान किया गया है।

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)

यहाँ, SQLite इंजन द्वारा मूल्यांकन की गई SQL अभिव्यक्ति निम्नानुसार होगी -

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 (?, ?)

उपरोक्त कोड के लिए आउटपुट निम्नानुसार है -

ID: 1 Name: Ravi Kumar Address: Station Road Nanded Email: [email protected]
ID: 3 Name: Rajender Nath Address: Sector 40, Gurgaon Email: [email protected]

तथा

यह संयोजन या तो उत्पन्न होता है putting multiple commas separated criteria in the filter or using and_() method जैसा कि नीचे दिया गया है -

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)

उपरोक्त दोनों दृष्टिकोण समान SQL अभिव्यक्ति में परिणाम करते हैं -

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 ?

कोड की उपरोक्त पंक्तियों के लिए आउटपुट है -

ID: 3 Name: Rajender Nath Address: Sector 40, Gurgaon Email: [email protected]

या

इस संयोजन द्वारा कार्यान्वित किया जाता है 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)

नतीजतन, SQLite इंजन समकक्ष SQL अभिव्यक्ति के बाद हो जाता है -

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 ?

उपरोक्त कोड के लिए आउटपुट निम्नानुसार है -

ID: 1 Name: Ravi Kumar Address: Station Road Nanded Email: [email protected]
ID: 3 Name: Rajender Nath Address: Sector 40, Gurgaon Email: [email protected]
ID: 4 Name: S.M.Krishna Address: Budhwar Peth, Pune Email: [email protected]