Contenido del Curso
SQL in Python Projects
SQL in Python Projects
Delete Operations
Delete operations in SQLite are used to remove records or data from a database table. These operations allow you to delete specific data that is no longer needed. Here's how you can perform delete operations in SQLite.
Basic Delete Operation
The primary SQL statement for deleting data is the DELETE
statement. Here's a basic example:
In this example, we:
- Connect to the database;
- Create a
cursor
to execute SQL queries; - Use a
DELETE
query to remove a specific record from the"articles"
table, identified by its"id"
; - Execute the query with
cursor.execute(delete_query)
; - Save the changes to the database using
connection.commit()
; - Finally, close the database connection.
Deleting Multiple Records
To delete multiple records that match certain criteria, you can use a WHERE
clause in the DELETE
query:
This query will delete all records where the author matches 'John Doe'
.
Deleting All Records
You can delete all records from a table by omitting the WHERE
clause:
This query will delete all records in the "articles"
table.
Deleting with Variables
You can use variables in your DELETE
queries to delete data dynamically. Here's an example of how to do that:
This query will delete the record with an "id"
equal to the value stored in the record_id
variable.
These are the basic concepts for performing delete operations in SQLite using Python. You can customize your DELETE
queries to remove specific data from your database as needed.
¡Gracias por tus comentarios!