SQLAlchemy SingleStoreDB Dialect

1.2.1 · active · verified Thu Apr 16

SQLAlchemy SingleStoreDB is a dialect for the SingleStoreDB database, enabling SQLAlchemy applications to connect and interact with SingleStoreDB. It provides support for SingleStoreDB-specific features like shard keys, sort keys, persisted columns, and vector data types. The current version is 1.2.1, and it typically releases new versions every few months to add features or address issues.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a connection to SingleStoreDB using SQLAlchemy, create a table, insert data, and select data. It uses environment variables for secure connection string management and handles basic error reporting.

import os
from sqlalchemy import create_engine, text

# Get connection details from environment variables for security
user = os.environ.get('SINGLESTORE_USER', 'admin')
password = os.environ.get('SINGLESTORE_PASSWORD', 'password')
host = os.environ.get('SINGLESTORE_HOST', '127.0.0.1')
port = os.environ.get('SINGLESTORE_PORT', '3306')
database = os.environ.get('SINGLESTORE_DATABASE', 'test_db')

# Construct connection URL
connection_url = f"singlestoredb://{user}:{password}@{host}:{port}/{database}"

try:
    # Create an engine instance
    engine = create_engine(connection_url)

    # Establish a connection and execute a simple query
    with engine.connect() as connection:
        # Example: Create a table if it doesn't exist
        connection.execute(text("CREATE TABLE IF NOT EXISTS my_table (id INT, name VARCHAR(255))"))
        print("Table 'my_table' ensured.")

        # Example: Insert data
        connection.execute(text("INSERT INTO my_table (id, name) VALUES (:id, :name)"), {"id": 1, "name": "Alice"})
        connection.execute(text("INSERT INTO my_table (id, name) VALUES (:id, :name)"), {"id": 2, "name": "Bob"})
        print("Data inserted.")

        # Example: Select data
        result = connection.execute(text("SELECT id, name FROM my_table"))
        for row in result:
            print(f"ID: {row.id}, Name: {row.name}")
        
        # Commit the changes (if not in autocommit mode, depends on dialect/DB config)
        connection.commit()

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →