Skip to content

Day 36: Dropping a table

Deleting a Table in SQLite using Python

In Python, if you have created a table in SQLite but no longer need it, you can remove it using the 'DROP TABLE' SQL command. However, be cautious when using this command, as it permanently deletes the table and all its data, which cannot be recovered.

Here's how you can delete a table in SQLite using Python:

import sqlite3

# Connect to the SQLite database
conn = sqlite3.connect("bookstore.db")

# Create a cursor to execute SQL commands
cursor = conn.cursor()

# Drop the 'books' table from the 'bookstore' database
cursor.execute("DROP TABLE books")

# Commit changes and close the connection
conn.commit()
conn.close()

In this example, the 'books' table from the 'bookstore' database is deleted. Remember to commit your changes using conn.commit() and close the connection using conn.close() after executing your SQL commands.

Learn More

Want to learn more about SQL for Machine Learning? Check out the full course HERE.


Need help mastering Machine Learning?

Don't just follow along — join me! Get exclusive access to me, your instructor, who can help answer any of your questions. Additionally, get access to a private learning group where you can learn together and support each other on your AI journey.