All Products
Search
Document Center

Lindorm:Use Python DB-API to develop applications

Last Updated:Mar 28, 2026

Connect to LindormTable using the phoenixdb library and run SQL operations from your Python application.

Prerequisites

Before you begin, ensure that you have:

Limitations

Python DB-API cannot connect to LindormTable on a Lindorm Serverless instance.

Install phoenixdb

pip install phoenixdb==1.2.0

Connect to LindormTable

Step 1: Set connection parameters

connect_kw_args = {
    'lindorm_user': '<userName>',
    'lindorm_password': '<userPassword>',
    'database': '<database>'
}
ParameterDescription
lindorm_userUsername for the Lindorm instance. To reset the password, see Change the password.
lindorm_passwordPassword for the Lindorm instance. To reset the password, see Change the password.
databaseName of the database to connect to. If omitted, the connection defaults to the default database.

Step 2: Open the connection

database_url = '<lindorm_sql_url>'

connection = phoenixdb.connect(database_url, autocommit=True, **connect_kw_args)
ParameterDescription
database_urlThe SQL endpoint for LindormTable. To find the endpoint, see View endpoints. Use the virtual private network (VPC) endpoint if your client runs on an Elastic Compute Service (ECS) instance in the same VPC as your Lindorm instance; otherwise, enable and use the public endpoint. Remove any string that appears before http in the endpoint copied from the Lindorm console. Example: http://ld-bp10m54739kg9****-proxy-lindorm.lindorm.rds.aliyuncs.com:30060
autocommitMust be set to True.
**connect_kw_argsThe connection parameters defined in Step 1.

Run DDL and DML operations

The following examples use a cursor to run DDL and DML operations on LindormTable. Each operation is shown as a standalone snippet so you can copy only what you need.

Create a table

with connection.cursor() as statement:
    sql = "CREATE TABLE IF NOT EXISTS test_python (c1 INTEGER, c2 INTEGER, PRIMARY KEY (c1))"
    statement.execute(sql)

Insert a single row

with connection.cursor() as statement:
    sql = "UPSERT INTO test_python (c1, c2) VALUES (1, 1)"
    statement.execute(sql)

Insert multiple rows

Use executemany() to insert multiple rows in a single call.

with connection.cursor() as statement:
    sql = "UPSERT INTO test_python (c1, c2) VALUES (?, ?)"
    statement.executemany(sql, [(2, 2), (3, 3)])

Update a row

with connection.cursor() as statement:
    sql = "UPSERT INTO test_python (c1, c2) VALUES (1, 10)"
    statement.execute(sql)

Delete rows

with connection.cursor() as statement:
    sql = "DELETE FROM test_python WHERE c1 = 2"
    statement.execute(sql)

Query data

fetchall() returns a list of tuples, one per row.

with connection.cursor() as statement:
    statement.execute("SELECT * FROM test_python")
    rows = statement.fetchall()
    for row in rows:
        print(row)   # e.g. (1, 10)

Drop a table

If your LindormTable version is later than 2.2.16 and earlier than 2.4.1, run OFFLINE TABLE before dropping the table.
with connection.cursor() as statement:
    # Required only for LindormTable versions > 2.2.16 and < 2.4.1
    statement.execute("OFFLINE TABLE test_python")
    statement.execute("DROP TABLE IF EXISTS test_python")

Close the connection

connection.close()
For the complete sample code, see the Python example on GitHub.

What's next