SingleStoreDB Python Client
The SingleStoreDB Python Client is a DB-API 2.0 compliant database connector that provides an interface to the SingleStoreDB database and its workspace management APIs. It supports Python 3.9+ and is designed for high-performance interactions with SingleStoreDB, including real-time analytics and vector search. The library maintains a frequent release cadence, typically with minor updates and patches released monthly or bi-monthly.
Warnings
- breaking The query parameter substitution syntax changed significantly in v0.5.0. Positional parameters changed from `:1` to `%s`, and named parameters from `:key` to `%(key)s`.
- breaking The `results_format` connection parameter was renamed to `results_type` in v0.5.0.
- gotcha Not using parameterized queries (e.g., f-strings or direct string concatenation) can lead to SQL injection vulnerabilities and incorrect query parsing.
- gotcha Failing to close database connections and cursors can lead to resource exhaustion and performance issues.
- breaking Minimum Python version requirement was raised to 3.8 in v0.8.0. Current version 1.16.9 requires Python 3.9+.
Install
-
pip install singlestoredb -
pip install 'singlestoredb[sqlalchemy]' -
pip install 'singlestoredb[dataframe]'
Imports
- connect
import singlestoredb as s2 conn = s2.connect(...)
- Connection
from singlestoredb import Connection # (less common, usually obtained from s2.connect())
- Cursor
from singlestoredb import Cursor # (less common, usually obtained from conn.cursor())
Quickstart
import singlestoredb as s2
import os
# Get connection details from environment variables for security
HOST = os.environ.get('SINGLESTOREDB_HOST', '127.0.0.1')
PORT = int(os.environ.get('SINGLESTOREDB_PORT', '3306'))
USER = os.environ.get('SINGLESTOREDB_USER', 'root')
PASSWORD = os.environ.get('SINGLESTOREDB_PASSWORD', 'password')
DATABASE = os.environ.get('SINGLESTOREDB_DATABASE', 'test_db')
try:
# Establish a connection
with s2.connect(
host=HOST,
port=PORT,
user=USER,
password=PASSWORD,
database=DATABASE
) as conn:
print("Successfully connected to SingleStoreDB!")
# Create a cursor object
with conn.cursor() as cur:
# Execute a query
cur.execute("CREATE TABLE IF NOT EXISTS my_table (id INT, name VARCHAR(255))")
cur.execute("INSERT INTO my_table (id, name) VALUES (%s, %s)", (1, 'Alice'))
conn.commit()
print("Table created and data inserted.")
# Fetch results
cur.execute("SELECT * FROM my_table")
for row in cur.fetchall():
print(row)
except s2.Error as e:
print(f"Database error: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")