शुरू करेंमुफ़्त में शुरू करें

टेबल से सभी रिकॉर्ड्स डिलीट करना

अक्सर, आपको किसी टेबल के सभी रिकॉर्ड्स खाली करने पड़ते हैं ताकि आप डेटा को फिर से लोड कर सकें. आप यह काम केवल टेबल को आर्ग्यूमेंट देकर 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())
कोड संपादित करें और चलाएँ