Python Data Persistence – Core – Updating Records

Python Data Persistence – Core – Updating Records

SQLalchemy’s core API defines the update ( ) function which lets the value of one or more columns in one or more rows be modified.

table.update( ).where(condition).values(Col=newval)

For example, to update the price of the TV to 45000, use

qry=Products.update().where(Products.c.name=="TV"). 
values(name=45000) 
con.execute(qry)

Similarly, to delete a certain record from the table, use the following statement:

qry=Products.delete().where(Products.c.name='TV') con.execute(qry)

At the outset, there appears to be some overlap among the usage patterns of the ORM and the Expression Language. However, the similarity is rather superficial. ORM approach is from the perspective of a user-defined domain model. SQL Expression Language looks at it from the perspective of literal schema and SQL expression representations.

While an application may use either approach exclusively, sometimes, in advanced scenarios, it may have to make occasional usage of the Expression Language directly in an otherwise ORM-oriented application where specific database interactions are required.

SQLAlchemy library is extensively used in Python-based web frameworks such as Flask and bottle. There are Flask-SQLAlchemy and Bottle_ SQLAlchemy extensions specifically written for them. Other popular ORM libraries are SQLObject and Django ORM.