Python
1. Prerequisites
The application using LeanXcale driver should meet the following prerequisites:
-
Python 3.8 or later is installed.
Samples use pip 23.0.1 or later.
2. How to Install the Driver and Write a CRUD
2.1. Introduction
LeanXcale is a 2003 ANSI SQL database. LeanXcale provides fast inserting/updating, real-time aggregation, and linear and transactional horizontal scalability.
LeanXcale provides a DBAPI driver based on PEP 249 – Python Database API Specification v2.0. to access. The following examples show us a quick start for PYTHON developers.
In this documentation, we focus on providing practical examples for performing fundamental operations on a LeanXcale database.
2.2. Driver Deploy
In order to use the LeanXcale DBAPI driver available from the LeanXcale pypi.org repository, you need to execute the following command:
pip3 install pyLeanxcale==2.0.0
Alternatively, you can manually download the driver from here and install it with pip3. Then, execute it by running:
pip3 install pyLeanXcale-2.0.0-py3-none-any.whlç
2.3. Connect
In order to establish a connection to the LeanXcale SQL interface, you need to provide the following parameters within your DBAPI URL:
IP/hostname (e.g., 123.45.67.89)
Port (default: 14420)
Database name (default: db)
Username (default: lxadmin)
Password (default: foobar)
The initial stage in setting up a connection between SQLAlchemy and LeanXcale involves the creation of an engine. This engine is configured with a particular dialect, which instructs it on how to interact with the underlying DBAPI.
import lxdbapi
url = 'leanxcale://lxadmin@123.45.67.89:14420/db'
conn = lxpy.connect(url, password='foobar')
try:
conn = engine.raw_connection()
# Use conn as connection to access a LeanXcale database
finally:
# To close the connection
conn.close()
2.4. Create, Alter, Drop a Table
For the creation, modification, or deletion of a database object, such as a table or sequence, use the execute() method.
2.4.1. Create
Below, you’ll find an example of how to create a table:
createTable = "CREATE TABLE person (
id BIGINT NOT NULL,
name VARCHAR,
lastName VARCHAR,
birth_date DATE,
passport VARCHAR,
phone VARCHAR,
PRIMARY KEY (id)
)"
try:
cursor = conn.cursor()
cursor.execute(createTable)
conn.commit()
except Exception as e:
print("Error:", e)
finally:
cursor.close()
2.5. Insert, Select, Update, and Delete rows
For data manipulation tasks, encompassing operations like inserting, updating, or deleting records, the tool at your disposal is the executeUpdate() method. This method serves the purpose of executing these SQL statements, and in turn, it provides valuable feedback - specifically, the count of rows influenced by your insert, update, or delete action.
2.5.1. Insert
This is how you add a new record:
try:
cursor = conn.cursor()
sql = "INSERT INTO person (id, name, lastName, birth_date, passport, phone) VALUES (%s, %s, %s, %s, %s, %s)"
cursor.execute(sql, (id_to_insert, name_to_insert, lastName_to_insert, birth_date_to_insert, passport_to_insert, phone_to_insert))
conn.commit()
print("Row inserted successfully")
except Exception as e:
print("Error:", e)
finally:
cursor.close()
2.5.2. Upsert
LeanXcale provides upserts. Unlike traditional inserts, upserts do not perform a prior check for record existence. Instead, they directly proceed to either insert or update the description, resulting in faster performance.
try:
cursor = conn.cursor()
sql = "UPSERT INTO person (id, name, lastName, birth_date, passport, phone) VALUES (%s, %s, %s, %s, %s, %s)"
cursor.execute(sql, (id_to_insert, name_to_insert, lastName_to_insert, birth_date_to_insert, passport_to_insert, phone_to_insert))
conn.commit()
print("Row inserted successfully")
except Exception as e:
print("Error:", e)
finally:
cursor.close()
2.5.3. Select
When interacting with the database using SQL statements, leverage either a Statement or PreparedStatement instance. Once you have prepared the Statement or PreparedStatement, execute the query, which will return a ResultSet instance. This ResultSet instance allows you to retrieve the complete result of your query.
try:
cursor = con.cursor()
cursor.execute('select * from person WHERE lastName = "Doe"')
for row in cursor.fetchall():
print(row)
except Exception as e:
print("Error:", e)
finally:
cursor.close()
2.5.4. Join
In this case, it collects data from several tables by executing a single query:
try:
cursor = con.cursor()
cursor.execute('SELECT title, name, isbn FROM author INNER JOIN book ON author.id = book.authorid ')
for row in cursor.fetchall():
print(row)
except Exception as e:
print("Error:", e)
finally:
cursor.close()
2.5.5. Update
Here is how to update existing records:
try:
cursor = conn.cursor()
sql = "UPDATE person SET passport = '666668888' WHERE id in (100, 101)"
cursor.execute(sql)
conn.commit()
except Exception as e:
print("Error:", e)
finally:
cursor.close()
2.5.6. Delete
This is the procedure for deleting records:
try:
cursor = conn.cursor()
sql = "DELETE FROM person WHERE id = %s"
cursor.execute(sql, (id_to_delete,))
rows_deleted = cursor.rowcount
conn.commit()
print(f"{rows_deleted} rows deleted")
except Exception as e:
print("Error:", e)
finally:
cursor.close()
These are the fundamental operations you can perform with the LeanXcale JDBC driver. For more advanced functionality and the optimization of your database interactions according to your specific requirements, we encourage you to delve deeper into LeanXcale’s capabilities:
-
On "How to Ingest Effectively" you will find a guide on effective data ingestion using LeanXcale.
-
"How to Read Optimally" will teach you how to maximize reading efficiency.
-
Refer to the Advance Deployment section for insights into specific details related to Python and its integration with LeanXcale.
-
You can access the comprehensive SQL reference documentation here.