Connect to LindormTable using the phoenixdb library and run SQL operations from your Python application.
Prerequisites
Before you begin, ensure that you have:
Python 3.7 or later installed
Your client IP address added to the Lindorm whitelist
Limitations
Python DB-API cannot connect to LindormTable on a Lindorm Serverless instance.
Install phoenixdb
pip install phoenixdb==1.2.0Connect to LindormTable
Step 1: Set connection parameters
connect_kw_args = {
'lindorm_user': '<userName>',
'lindorm_password': '<userPassword>',
'database': '<database>'
}| Parameter | Description |
|---|---|
lindorm_user | Username for the Lindorm instance. To reset the password, see Change the password. |
lindorm_password | Password for the Lindorm instance. To reset the password, see Change the password. |
database | Name 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)| Parameter | Description |
|---|---|
database_url | The 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 |
autocommit | Must be set to True. |
**connect_kw_args | The 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
For more information, see SQL reference for LindormTable.