All Products
Search
Document Center

Lindorm:Develop applications based on a Python ORM framework

Last Updated:Mar 28, 2026

Connect to LindormTable using SQLAlchemy and perform CRUD operations through its MySQL-compatible interface. This guide covers installing dependencies, configuring a connection, defining a model, and running insert, query, update, and delete operations.

Prerequisites

Before you begin, make sure you have:

Install dependencies

LindormTable exposes a MySQL-compatible endpoint. Connecting to it from Python requires two libraries:

  • SQLAlchemy: the ORM layer that lets you map Python classes to database tables and write queries in Python instead of raw SQL

  • PyMySQL: the database driver that handles the underlying MySQL protocol communication; SQLAlchemy does not include a driver

Install both:

pip install PyMySQL
pip install SQLAlchemy

Configure the connection

Use create_engine() to initialize a connection to your LindormTable instance.

from sqlalchemy import create_engine

engine = create_engine(
    'mysql+pymysql://<user>:<password>@<lindorm_mysql_addr>:33060/<database>',
    echo=True
)
ParameterDescription
<user>Your LindormTable username. To reset a forgotten password, see Modify a user password
<password>The password for the user. To reset a forgotten password, see Modify a user password
<lindorm_mysql_addr>The MySQL-compatible endpoint. See View endpoints
<database>The target database name. Defaults to default

Choosing your endpoint type:

DeploymentEndpoint to useHow to get it
Application on an ECS instance (recommended)MySQL-compatible VPC endpoint — provides higher security and lower network latencySee View endpoints
Application deployed on-premisesMySQL-compatible Internet endpointBefore connecting, enable the public endpoint in the Lindorm console: go to Database Connections > Wide Table Engine, then click Enable Public Endpoint

Define a model

Define your table structure as a Python class using SQLAlchemy's declarative base.

from sqlalchemy import Column, String, Integer, Float
from sqlalchemy.orm import declarative_base

Base = declarative_base()

class Player(Base):
    __tablename__ = 'player'
    player_id = Column(Integer, primary_key=True, autoincrement=False)
    player_name = Column(String(255))
    player_height = Column(Float)

    def __str__(self):
        return " ".join(str(item) for item in (self.player_id, self.player_name, self.player_height))

Perform CRUD operations

All examples use a SQLAlchemy session to interact with LindormTable.

Create the table and insert data

from sqlalchemy.orm import sessionmaker

# Create the table based on the model definition
Base.metadata.create_all(engine)

Session = sessionmaker(bind=engine)
session = Session()

# Insert a single row
new_player = Player(player_id=1001, player_name="john", player_height=2.08)
session.add(new_player)

# Insert multiple rows
session.add_all([
    Player(player_id=1002, player_name="bob", player_height=1.65),
    Player(player_id=1003, player_name="smith", player_height=1.82),
])
session.commit()

Query data

# Query rows matching a filter condition
rows = session.query(Player).filter(Player.player_id == 1001).all()
print([row.__str__() for row in rows])

Update data

# Update a field for rows matching a filter condition
session.query(Player).filter(Player.player_id == 1003).update({"player_name": "brown"})
session.commit()

Delete data

# Delete rows matching a filter condition
session.query(Player).filter(Player.player_id == 1002).delete()
session.commit()

Complete sample code

The following is a complete working example. Replace the connection string placeholders with your actual values before running it.

from sqlalchemy import create_engine
from sqlalchemy import Column, String, Integer, Float
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import sessionmaker


Base = declarative_base()

# Define the Player model
class Player(Base):
    __tablename__ = 'player'
    player_id = Column(Integer, primary_key=True, autoincrement=False)
    player_name = Column(String(255))
    player_height = Column(Float)

    def __str__(self):
        return " ".join(str(item) for item in (self.player_id, self.player_name, self.player_height))


# Initialize the connection. Replace the placeholders with your actual values.
engine = create_engine(
    'mysql+pymysql://user:test@ld-bp1l39h99192d****-proxy-sql-lindorm-public.lindorm.rds.aliyuncs.com:33060/default',
    echo=True
)
Session = sessionmaker(bind=engine)

# Create the table
Base.metadata.create_all(engine)

# Open a session
session = Session()

# Insert a single row
new_player = Player(player_id=1001, player_name="john", player_height=2.08)
session.add(new_player)

# Insert multiple rows
session.add_all([
    Player(player_id=1002, player_name="bob", player_height=1.65),
    Player(player_id=1003, player_name="smith", player_height=1.82),
])
session.commit()

# Query rows where player_id is 1001
rows = session.query(Player).filter(Player.player_id == 1001).all()
print([row.__str__() for row in rows])

# Delete the row where player_id is 1002
session.query(Player).filter(Player.player_id == 1002).delete()
session.commit()

# Update the row where player_id is 1003
session.query(Player).filter(Player.player_id == 1003).update({"player_name": "brown"})
session.commit()

# Query all remaining rows
rows = session.query(Player).all()
print([row.__str__() for row in rows])

Expected output:

['1001 john 2.08']
['1001 john 2.08', '1003 brown 1.82']

What's next