Horje
Installing Sqlalchemy-Utils using Python Pip

In this article, we will explore how to Install Sqlalchemy-Utils Using Pip. To utilize Sqlalchemy-Utils functionalities, it is essential to Install Sqlalchemy-Utils Using Pip and this can be achieved by following the steps outlined below.

What is Sqlalchemy-Utils ?

SQLAlchemy-Utils is a library that provides various utility functions and extensions for SQLAlchemy, which is a popular SQL toolkit and Object-Relational Mapping (ORM) library for Python. SQLAlchemy-Utils extends the functionality of SQLAlchemy by offering additional features and tools to simplify common tasks when working with databases.

Installing Sqlalchemy-Utils Using Pip

Below, we will be able to explain step-by-step on how to Install Sqlalchemy-Utils Using Pip in Python.

Step 1: Install Sqlalchemy-Utils Library

Before using Sqlalchemy-Utils , it is necessary to install the Sqlalchemy-Utils library by executing the following command in the terminal:

pip install Sqlalchemy-Utils 

Output:

sqlachemy

Step 2: Show the Version of Sqlalchemy-Utils

Once Sqlalchemy-Utils is installed, you can check the version using below command. This not only makes your code more concise but also follows a widely adopted practice in the Python community.

pip show Sqlalchemy-Utils

Output:

second-slq

Use Case Example of Sqlalchemy-Utils

Enumerate Priority Levels with SQLAlchemy-Utils

In below code, establishes an SQLite in-memory database and defines a Task model using SQLAlchemy-Utils’ ChoiceType to represent enumerated priority levels. The ChoiceType ensures that the ‘priority’ column can only have values chosen from a predefined set: ‘Low’, ‘Medium’, and ‘High’. The declarative base class, declarative_base(), is utilized to create the base for the model, and the table is named ‘tasks’.

Python3

from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy_utils import ChoiceType
  
engine = create_engine("sqlite:///:memory:")
Base = declarative_base()
  
class Task(Base):
    __tablename__ = 'tasks'
    id = Column(Integer, primary_key=True)
    name = Column(String, nullable=False)
    priority = Column(ChoiceType(choices={'low': 'Low', 'medium': 'Medium', 'high': 'High'}), nullable=False)
  
Base.metadata.create_all(engine)

Output:

finally




Reffered: https://www.geeksforgeeks.org


Python

Related
Single Vs Double Quotes in Python Json Single Vs Double Quotes in Python Json
Make a Set of Lists in Python Make a Set of Lists in Python
Get the Length of Text Without Using Inbuild Python Library Function Get the Length of Text Without Using Inbuild Python Library Function
Search for Value in the Python Dictionary with Multiple Values for a Key Search for Value in the Python Dictionary with Multiple Values for a Key
Run One Python Script From Another in Python Run One Python Script From Another in Python

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
12