Some of the critical features utilized in SQLAlchemy are discussed on this chapter.
Standard SQL has advocated many functions which can be implemented by way of maximum dialects. They return a single value primarily based on the arguments exceeded to it. Some SQL features take columns as arguments while some are standard. Thefunc key-word in SQLAlchemy API is used to generate these capabilities.
In SQL, now() is a time-honored characteristic. Following statements renders the now() feature using func −
from sqlalchemy.sql import func
result = conn.execute(select([func.now()]))
print (result.fetchone())
Sample end result of above code can be as shown beneath −
(datetime.datetime(2018, 6, 16, 6, 4, 40),)
On the other hand, be counted() characteristic which returns range of rows selected from a table, is rendered by means of following usage of func −
from sqlalchemy.sql import func
result = conn.execute(select([func.count(students.c.id)]))
print (result.fetchone())
From the above code, rely of quantity of rows in college students desk can be fetched.
Some integrated SQL functions are demonstrated the usage of Employee table with following facts −
ID | Name | Marks |
---|---|---|
1 | Kamal | 56 |
2 | Fernandez | 85 |
3 | Sunil | 62 |
4 | Bhaskar | 76 |
The max() function is implemented by following usage of func from SQLAlchemy as a way to result in eighty five, the entire maximum marks received −
from sqlalchemy.sql import func
result = conn.execute(select([func.max(employee.c.marks)]))
print (result.fetchone())
Similarly, min() function that will return 56, minimum marks, could be rendered with the aid of following code −
from sqlalchemy.sql import func
result = conn.execute(select([func.min(employee.c.marks)]))
print (result.fetchone())
So, the AVG() feature also can be implemented by means of using the below code −
from sqlalchemy.sql import func
result = conn.execute(select([func.avg(employee.c.marks)]))
print (result.fetchone())
Functions are normally used in the columns clause of a select statement.
They can also be given label as well as a type. A label to function allows the result
to be targeted in a result row based on a string name, and a type is required when
you need result-set processing to occur.from sqlalchemy.sql import func
result = conn.execute(select([func.max(students.c.lastname).label('Name')]))
print (result.fetchone())
