SingleStoreDB Python Client

1.16.9 · active · verified Mon Apr 13

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

Install

Imports

Quickstart

This quickstart demonstrates how to connect to a SingleStoreDB instance using environment variables for credentials, create a table, insert data using parameterized queries, and fetch results. The `with` statements ensure connections and cursors are properly closed.

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}")

view raw JSON →