Your cart is currently empty!
Category: sqlalchemy
-
Khóa học miễn phí Parameter-Ordered Updates nhận dự án làm có lương
Parameter-Ordered Updates
The UPDATE query of raw SQL has SET clause. It is rendered by the update() construct using the column ordering given in the originating Table object. Therefore, a particular UPDATE statement with particular columns will be rendered the same each time. Since the parameters themselves are passed to the Update.values() method as Python dictionary keys, there is no other fixed ordering available.
In some cases, the order of parameters rendered in the SET clause are significant. In MySQL, providing updates to column values is based on that of other column values.
Following statement’s result −
UPDATE table1 SET x = y + 10, y = 20
will have a different result than −
UPDATE table1 SET y = 20, x = y + 10
SET clause in MySQL is evaluated on a per-value basis and not on per-row basis. For this purpose, the preserve_parameter_order is used. Python list of 2-tuples is given as argument to the Update.values() method −
stmt = table1.update(preserve_parameter_order = True). values([(table1.c.y, 20), (table1.c.x, table1.c.y + 10)])
The List object is similar to dictionary except that it is ordered. This ensures that the “y” column’s SET clause will render first, then the “x” column’s SET clause.
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc
Khóa học miễn phí Using Joins nhận dự án làm có lương
SQLAlchemy Core – Using Joins
In this chapter, we will learn how to use Joins in SQLAlchemy.
Effect of joining is achieved by just placing two tables in either the columns clause or the where clause of the select() construct. Now we use the join() and outerjoin() methods.
The join() method returns a join object from one table object to another.
join(right, onclause = None, isouter = False, full = False)
The functions of the parameters mentioned in the above code are as follows −
-
right − the right side of the join; this is any Table object
-
onclause − a SQL expression representing the ON clause of the join. If left at None, it attempts to join the two tables based on a foreign key relationship
-
isouter − if True, renders a LEFT OUTER JOIN, instead of JOIN
-
full − if True, renders a FULL OUTER JOIN, instead of LEFT OUTER JOIN
For example, following use of join() method will automatically result in join based on the foreign key.
>>> print(students.join(addresses))
This is equivalent to following SQL expression −
students JOIN addresses ON students.id = addresses.st_id
You can explicitly mention joining criteria as follows −
j = students.join(addresses, students.c.id == addresses.c.st_id)
If we now build the below select construct using this join as −
stmt = select([students]).select_from(j)
This will result in following SQL expression −
SELECT students.id, students.name, students.lastname FROM students JOIN addresses ON students.id = addresses.st_id
If this statement is executed using the connection representing engine, data belonging to selected columns will be displayed. The complete code is as follows −
from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String, ForeignKey engine = create_engine(''sqlite:///college.db'', echo = True) meta = MetaData() conn = engine.connect() students = Table( ''students'', meta, Column(''id'', Integer, primary_key = True), Column(''name'', String), Column(''lastname'', String), ) addresses = Table( ''addresses'', meta, Column(''id'', Integer, primary_key = True), Column(''st_id'', Integer,ForeignKey(''students.id'')), Column(''postal_add'', String), Column(''email_add'', String) ) from sqlalchemy import join from sqlalchemy.sql import select j = students.join(addresses, students.c.id == addresses.c.st_id) stmt = select([students]).select_from(j) result = conn.execute(stmt) result.fetchall()
The following is the output of the above code −
[ (1, ''Ravi'', ''Kapoor''), (1, ''Ravi'', ''Kapoor''), (3, ''Komal'', ''Bhandari''), (5, ''Priya'', ''Rajhans''), (2, ''Rajiv'', ''Khanna'') ]
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc