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

Contenido del Curso

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.

Tarea

  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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 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.

Tarea

  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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 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.

Tarea

  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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

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.

Tarea

  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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 4. Capítulo 7
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
some-alt