Selecting NULL values using SQLAlchemy in PostgreSQL
Using select() and where function, we can query data from our database. The NULL equivalent in python is None.
The other things remain the same, like conventional SQL querying. The query is passed to a execute() statement and output is stored in a result object and rows can be iterated to fetch all the rows
Code:
Python3
# write a conventional SQL query # with NULL equivalent as None s = book_publisher.select().where( book_publisher.c.publisherName = = None ) # output get stored in result object result = engine.execute(s) # iteratte through the result object # to get all rows of the output for row in result: print (row) |
Output:
As we know from the output shown above, row 6 has None values and that is correctly returned in the below output
(6, None, None)
Select NULL Values in SQLAlchemy
In this article, we will see how to select NULL values into a PostgreSQL database using SQLAlchemy in Python.
For demonstration purposes first, let us create a sample table using SQLAlchemy in PostgreSQL as shown below
Contact Us