VOOZH about

URL: https://www.geeksforgeeks.org/python/how-to-get-value-from-sqlalchemy-instance-by-column-name/

⇱ How to get value from SQLAlchemy instance by column name? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to get value from SQLAlchemy instance by column name?

Last Updated : 21 Feb, 2022

In this article, we will discuss how to get value from a table in SQL with the help of SQLAlchemy in Python.

Database used:

👁 Image

Installation

pip install SQLAlchemy pymysql

Note: pymysql is a dependency of sqlalchemy which users need to install so as to run our program.

Get value by column name

The given task can be performed by first creating an engine with sqlalchemy, connecting with the database, and executing an SQL query with the connection. The SQL query will contain the name of the column/columns whose values we want and then we will get a result object. The result object will give us all the values when we call fetchall method of the object instance.

Output:

👁 Image
Output

Example 2:

Here, if the user wants to get values of multiple columns then you can specify the column names in the query by separating them with a comma (,).

Output:

👁 Image
Output
Comment