Conteúdo do Curso
SQL in Python Projects
SQL in Python Projects
Update Operations
Update Operations
Update operations in SQLite involve modifying existing data in a database table. These operations are used to make changes to existing records based on specific criteria. Here's how you can perform update operations in SQLite.
Basic Update Operation
The primary SQL statement for updating data is the UPDATE
statement. Here's a basic example:
In this example, we:
- Connect to the database;
- Create a
cursor
to execute SQL queries; - Use an
UPDATE
query to modify the"title"
of a specific record in the"articles"
table, identified by its"id"
; - Execute the query with
cursor.execute(update_query)
; - Save the changes to the database using connection.commit();
- Finally, close the database connection.
Updating Multiple Records
To update multiple records that match certain criteria, you can use a WHERE
clause in the UPDATE
query:
This query will update the "title" for all records where the author matches 'John Doe'.
Updating Multiple Columns
You can also update multiple columns simultaneously by specifying multiple column assignments in the SET
clause:
This query will update both the "title" and "content" for the record with "id" equal to 1.
Updating with Variables
You can use variables in your UPDATE
queries to update data dynamically. Here's an example of how to do that:
This query will update the "title" of the record with "id" equal to the value stored in the record_id
variable.
These are the basic concepts for performing update operations in SQLite using Python. You can customize your UPDATE
queries to modify specific data in your database as needed.
Obrigado pelo seu feedback!