टेबल से सभी रिकॉर्ड्स डिलीट करना
अक्सर, आपको किसी टेबल के सभी रिकॉर्ड्स खाली करने पड़ते हैं ताकि आप डेटा को फिर से लोड कर सकें. आप यह काम केवल टेबल को आर्ग्यूमेंट देकर delete स्टेटमेंट से कर सकते हैं. उदाहरण के लिए, वीडियो में Jason ने extra_employees टेबल को इस तरह डिलीट किया:
delete_stmt = delete(extra_employees)
result_proxy = connection.execute(delete_stmt)
लेकिन सावधान रहें, डिलीट किया गया डेटा वापस नहीं लाया जा सकता!
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Databases का परिचय
अभ्यास निर्देश
- sqlalchemy से
deleteऔरselectइम्पोर्ट करें. censusटेबल से सारा डेटा हटाने के लिए एकdeleteस्टेटमेंट बनाएँ. इसेdelete_stmtके रूप में सेव करें.connectionके जरिएdelete_stmtको execute करें औरresultsमें सेव करें.- उत्तर सबमिट करें:
censusटेबल से बची हुई सभी rows कोselectकर के प्रिंट करें, ताकि पुष्टि हो कि टेबल अब खाली है!
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Import delete, select
from sqlalchemy import ____, ____
# Build a statement to empty the census table: stmt
delete_stmt = ____
# Execute the statement: results
results = ____
# Print affected rowcount
print(results.rowcount)
# Build a statement to select all records from the census table : select_stmt
select_stmt = select([census])
# Print the results of executing the statement to verify there are no rows
print(connection.execute(select_stmt).fetchall())