Introduction to Databases in Python
Jason Myers
Co-Author of Essential SQLAlchemy and Software Engineer
update()
statementinsert()
statement but includes a where
clause to determine what record will be updatedvalues()
clause as column=value
pairsfrom sqlalchemy import update
stmt = update(employees) stmt = stmt.where(employees.columns.id == 3) stmt = stmt.values(active=True)
result_proxy = connection.execute(stmt) print(result_proxy.rowcount)
1
where
clause that will select all the records you want to updatestmt = update(employees) stmt = stmt.where(employees.columns.active == True)
stmt = stmt.values(active=False, salary=0.00)
result_proxy = connection.execute(stmt) print(result_proxy.rowcount)
3
new_salary = select([employees.columns.salary]) new_salary = new_salary.order_by( desc(employees.columns.salary)) new_salary = new_salary.limit(1)
stmt = update(employees)
stmt = stmt.values(salary=new_salary)
result_proxy = connection.execute(stmt)
print(result_proxy.rowcount)
3
select()
statement to find the value for the column we are updatingIntroduction to Databases in Python