Table से चयनित डेटा को फ़िल्टर करें - Expressions
स्टैंडर्ड Python comparators के अलावा, हम in_() जैसे methods का भी उपयोग कर सकते हैं ताकि और शक्तिशाली where() क्लॉज़ बनाए जा सकें. आप expressions की पूरी सूची SQLAlchemy Documentation में देख सकते हैं.
किसी कॉलम पर इस्तेमाल होने पर, in_() method हमें वे रिकॉर्ड शामिल करने देता है जहाँ कॉलम का मान संभावित मानों की किसी सूची में आता हो. उदाहरण के लिए, where(census.columns.age.in_([20, 30, 40])) केवल उन लोगों के रिकॉर्ड लौटाएगा जिनकी उम्र ठीक 20, 30, या 40 साल है.
इस अभ्यास में, आप census टेबल पर ही काम जारी रखेंगे, और सबसे घनी आबादी वाले तीन राज्यों के लोगों के रिकॉर्ड चुनेंगे. उन राज्यों की सूची आपके लिए पहले से बना दी गई है.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Databases का परिचय
अभ्यास निर्देश
censusटेबल से सभी रिकॉर्ड select करें.whereक्लॉज़ के आर्ग्युमेंट को संशोधित करें ताकिin_()का उपयोग करके वे सभी रिकॉर्ड लौटें जहाँcensus.columns.stateकॉलम का मानstatesसूची में हो.- ResultProxy
connection.execute(stmt)पर लूप चलाएँ और हर रिकॉर्ड सेstateऔरpop2000कॉलम प्रिंट करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Define a list of states for which we want results
states = ['New York', 'California', 'Texas']
# Create a query for the census table: stmt
stmt = select(____)
# Append a where clause to match all the states in_ the list states
stmt = stmt.where(____)
# Loop over the ResultProxy and print the state and its population in 2000
for ____ in connection.execute(____):
print(____, ____)