Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Update | SQLAlchemy
Databases in Python
course content

Course Content

Databases in Python

Databases in Python

1. Introduction to SQLite
2. CRUD
3. More About SQLite
4. SQLAlchemy

bookUpdate

In this chapter, we’ll learn how to update records in a database using SQLAlchemy. Updating records is crucial when modifying existing data in your tables, such as changing descriptions or other fields. SQLAlchemy provides efficient and straightforward methods for performing updates.

Updating a Single Record

The most common update operation is modifying a single record. To do this, you need to fetch the object, update its attributes, and then save the changes.

To retrieve a product by its ID, you use a query with a filter to specify the desired ID. After updating the product's description, calling session.commit() saves the changes to the database.

Updating Multiple Records

Sometimes, you need to update multiple records at once. SQLAlchemy allows you to use filter() with the update() method to modify records efficiently.

To filter products priced above $1000, you use a query with a condition on the price. The update method applies the desired changes to all matching records, while the synchronize_session="fetch" argument ensures the session remains synchronized after the update.

Bulk Updates

For large-scale changes, bulk updates are more efficient as they directly modify database records without loading objects into memory. This makes them ideal for updating many rows at once.

This code efficiently updates the description for all products priced below $500 in a single operation. Bulk updates are faster and conserve memory compared to updating records individually.

Task

  1. Retrieve all products from the database.
  2. Calculate the new price for each product by reducing it by 20%.
  3. Save the updated prices to the database.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 7
toggle bottom row

bookUpdate

In this chapter, we’ll learn how to update records in a database using SQLAlchemy. Updating records is crucial when modifying existing data in your tables, such as changing descriptions or other fields. SQLAlchemy provides efficient and straightforward methods for performing updates.

Updating a Single Record

The most common update operation is modifying a single record. To do this, you need to fetch the object, update its attributes, and then save the changes.

To retrieve a product by its ID, you use a query with a filter to specify the desired ID. After updating the product's description, calling session.commit() saves the changes to the database.

Updating Multiple Records

Sometimes, you need to update multiple records at once. SQLAlchemy allows you to use filter() with the update() method to modify records efficiently.

To filter products priced above $1000, you use a query with a condition on the price. The update method applies the desired changes to all matching records, while the synchronize_session="fetch" argument ensures the session remains synchronized after the update.

Bulk Updates

For large-scale changes, bulk updates are more efficient as they directly modify database records without loading objects into memory. This makes them ideal for updating many rows at once.

This code efficiently updates the description for all products priced below $500 in a single operation. Bulk updates are faster and conserve memory compared to updating records individually.

Task

  1. Retrieve all products from the database.
  2. Calculate the new price for each product by reducing it by 20%.
  3. Save the updated prices to the database.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 7
toggle bottom row

bookUpdate

In this chapter, we’ll learn how to update records in a database using SQLAlchemy. Updating records is crucial when modifying existing data in your tables, such as changing descriptions or other fields. SQLAlchemy provides efficient and straightforward methods for performing updates.

Updating a Single Record

The most common update operation is modifying a single record. To do this, you need to fetch the object, update its attributes, and then save the changes.

To retrieve a product by its ID, you use a query with a filter to specify the desired ID. After updating the product's description, calling session.commit() saves the changes to the database.

Updating Multiple Records

Sometimes, you need to update multiple records at once. SQLAlchemy allows you to use filter() with the update() method to modify records efficiently.

To filter products priced above $1000, you use a query with a condition on the price. The update method applies the desired changes to all matching records, while the synchronize_session="fetch" argument ensures the session remains synchronized after the update.

Bulk Updates

For large-scale changes, bulk updates are more efficient as they directly modify database records without loading objects into memory. This makes them ideal for updating many rows at once.

This code efficiently updates the description for all products priced below $500 in a single operation. Bulk updates are faster and conserve memory compared to updating records individually.

Task

  1. Retrieve all products from the database.
  2. Calculate the new price for each product by reducing it by 20%.
  3. Save the updated prices to the database.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

In this chapter, we’ll learn how to update records in a database using SQLAlchemy. Updating records is crucial when modifying existing data in your tables, such as changing descriptions or other fields. SQLAlchemy provides efficient and straightforward methods for performing updates.

Updating a Single Record

The most common update operation is modifying a single record. To do this, you need to fetch the object, update its attributes, and then save the changes.

To retrieve a product by its ID, you use a query with a filter to specify the desired ID. After updating the product's description, calling session.commit() saves the changes to the database.

Updating Multiple Records

Sometimes, you need to update multiple records at once. SQLAlchemy allows you to use filter() with the update() method to modify records efficiently.

To filter products priced above $1000, you use a query with a condition on the price. The update method applies the desired changes to all matching records, while the synchronize_session="fetch" argument ensures the session remains synchronized after the update.

Bulk Updates

For large-scale changes, bulk updates are more efficient as they directly modify database records without loading objects into memory. This makes them ideal for updating many rows at once.

This code efficiently updates the description for all products priced below $500 in a single operation. Bulk updates are faster and conserve memory compared to updating records individually.

Task

  1. Retrieve all products from the database.
  2. Calculate the new price for each product by reducing it by 20%.
  3. Save the updated prices to the database.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 4. Chapter 7
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt