Source From Here
Using UPDATE Expression
The update() method on target table object constructs equivalent UPDATE SQL expression:
The
values() method on the resultant update object is used to specify the SET conditions of the UPDATE. If left as None, the SET conditions are determined from those parameters passed to the statement during the execution and/or compilation of the statement; The where clause is an Optional expression describing the WHERE condition of the UPDATE statement.
Following code snippet changes value of ‘lastname’ column from ‘Khanna’ to ‘Kapoor’ in students table:
The bound parameter lastname_1 will be substituted when execute() method is invoked. The complete update code is given below:
- demo6.py
Execution result:
Note that similar functionality can also be achieved by using update() function in sqlalchemy.sql.expression module as shown below:
DELETE Expression
In the previous chapter, we have understood what an Update expression does. The next expression that we are going to learn is Delete expression. The delete operation can be achieved by running delete() method on target table object as given in the following statement:
In case of students table, the above line of code constructs a SQL expression as following:
However, this will delete all rows in students table. Usually
DELETE query is associated with a logical expression specified by WHERE clause. The following statement shows where parameter:
The resultant SQL expression will have a bound parameter which will be substituted at runtime when the statement is executed:
Following code example will delete those rows from students table having lastname as ‘Khanna’:
- demo7.py
Using UPDATE Expression
The update() method on target table object constructs equivalent UPDATE SQL expression:
- table.update().where(conditions).values(SET expressions)
Following code snippet changes value of ‘lastname’ column from ‘Khanna’ to ‘Kapoor’ in students table:
The bound parameter lastname_1 will be substituted when execute() method is invoked. The complete update code is given below:
- demo6.py
- from sqlalchemy.sql import alias, select
- from sqlalchemy import text
- from sqlalchemy import create_engine, MetaData
- db_string = "postgresql://postgres:<yourpassword>@localhost/testdb"
- engine = create_engine(db_string, echo = True)
- meta = MetaData(bind=engine)
- meta.reflect()
- conn = engine.connect()
- students = meta.tables['students']
- update_stmt = students.update().where(students.c.lastname == 'Khanna').values(lastname='Kapoor')
- conn.execute(update_stmt)
- select_stmt = students.select().where(students.c.lastname == 'Kapoor')
- result = conn.execute(select_stmt).fetchone()
- print(result)
Note that similar functionality can also be achieved by using update() function in sqlalchemy.sql.expression module as shown below:
- from sqlalchemy.sql.expression import update
- stmt = update(students).where(students.c.lastname == 'Khanna').values(lastname = 'Kapoor')
In the previous chapter, we have understood what an Update expression does. The next expression that we are going to learn is Delete expression. The delete operation can be achieved by running delete() method on target table object as given in the following statement:
- stmt = students.delete()
- 'DELETE FROM students'
- stmt = students.delete().where(students.c.id > 2)
- 'DELETE FROM students WHERE students.id > :id_1'
- demo7.py
- from sqlalchemy.sql import alias, select
- from sqlalchemy import text
- from sqlalchemy import create_engine, MetaData
- from pprint import pprint
- db_string = "postgresql://postgres:<yourpassword≷@localhost/testdb"
- engine = create_engine(db_string, echo = True)
- meta = MetaData(bind=engine)
- meta.reflect()
- conn = engine.connect()
- students = meta.tables['students']
- delete_stmt = students.delete().where(students.c.lastname == 'Khanna')
- conn.execute(delete_stmt)
- s = students.select()
- result = conn.execute(s).fetchall()
- pprint(result)
This message was edited 14 times. Last update was at 08/03/2019 13:40:57
沒有留言:
張貼留言