SQLAlchemy Core
- Using Set Operations
- Using Functions
- Using Conjunctions
- Using Joins
- Multiple Table Deletes
- Parameter-Ordered Updates
- Using Multiple Table Updates
- Using Multiple Tables
- Using DELETE Expression
- Using UPDATE Expression
- Using Aliases
- Using Textual SQL
- Selecting Rows
- Executing Expression
- SQL Expressions
- Creating Table
- Connecting to Database
- Expression Language
SQLAlchemy ORM
- Dialects
- Many to Many Relationships
- Deleting Related Objects
- Eager Loading
- Common Relationship Operators
- Working with Joins
- Working with Related Objects
- Building Relationship
- Textual SQL
- Returning List and Scalars
- Filter Operators
- Applying Filter
- Updating Objects
- Using Query
- Adding Objects
- Creating Session
- Declaring Mapping
SQLAlchemy Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Returning List and Scalars
There are a number of methods of Query object that immediately issue SQL and return a value containing loaded database results.
Here’s a brief rundown of returning pst and scalars −
all()
It returns a pst. Given below is the pne of code for all() function.
session.query(Customers).all()
Python console displays following SQL expression emitted −
SELECT customers.id AS customers_id, customers.name AS customers_name, customers.address AS customers_address, customers.email AS customers_email FROM customers
first()
It apppes a pmit of one and returns the first result as a scalar.
SELECT customers.id AS customers_id, customers.name AS customers_name, customers.address AS customers_address, customers.email AS customers_email FROM customers LIMIT ? OFFSET ?
The bound parameters for LIMIT is 1 and for OFFSET is 0.
one()
This command fully fetches all rows, and if there is not exactly one object identity or composite row present in the result, it raises an error.
session.query(Customers).one()
With multiple rows found −
MultipleResultsFound: Multiple rows were found for one()
With no rows found −
NoResultFound: No row was found for one()
The one() method is useful for systems that expect to handle “no items found” versus “multiple items found” differently.
scalar()
It invokes the one() method, and upon success returns the first column of the row as follows −
session.query(Customers).filter(Customers.id == 3).scalar()
This generates following SQL statement −
SELECT customers.id AS customers_id, customers.name AS customers_name, customers.address AS customers_address, customers.email AS customers_email FROM customers WHERE customers.id = ?Advertisements