Install the Python connector and start a Polypheny server with the plain Prism transport enabled. The examples read POLYPHENY_HOST, POLYPHENY_PORT, POLYPHENY_USER and POLYPHENY_PASSWORD. Their defaults are 127.0.0.1, 20590, pa and an empty password. The account needs permission to create and drop a table in the default relational namespace.
Run SQL and manage a transaction
connect() returns a connection. Create a cursor with connection.cursor(). Use ? placeholders for values and pass the parameters separately. This example uses a generated table name, commits two inserts and rolls back a third.
import os
from uuid import uuid4
import polypheny
connection = polypheny.connect(
(os.getenv("POLYPHENY_HOST", "127.0.0.1"), int(os.getenv("POLYPHENY_PORT", "20590"))),
username=os.getenv("POLYPHENY_USER", "pa"),
password=os.getenv("POLYPHENY_PASSWORD", ""),
transport="plain",
)
table = f"py_items_{uuid4().hex[:12]}"
cursor = connection.cursor()
try:
cursor.execute(f"CREATE TABLE {table} (id INTEGER PRIMARY KEY, label VARCHAR(40))")
cursor.executemany(
f"INSERT INTO {table} (id, label) VALUES (?, ?)",
[(1, "Apple"), (2, "Pear")],
)
connection.commit()
cursor.execute(f"INSERT INTO {table} (id, label) VALUES (?, ?)", (3, "Plum"))
connection.rollback()
cursor.execute(f"SELECT id, label FROM {table} WHERE id >= ? ORDER BY id", (1,))
print(cursor.fetchone())
print(cursor.fetchall())
finally:
try:
connection.rollback()
cursor.execute(f"DROP TABLE IF EXISTS {table}")
finally:
connection.close()
fetchone() returns the next row or None after the result is exhausted. fetchall() returns the remaining rows as a list. You can also iterate over the cursor or read a batch with fetchmany().
Auto-commit starts disabled. Commit changes explicitly with connection.commit() or discard pending changes with connection.rollback(). DDL, including CREATE TABLE and DROP TABLE, commits the transaction automatically. Keep DDL outside a group of changes you may roll back. Closing a connection closes its cursors and rolls back pending changes.
Query with MQL
Use executeany() to select a language or a namespace for a statement. The next example uses the MQL plugin to read a relational table in the default public namespace as documents. Each document is returned as a dictionary.
import os
from uuid import uuid4
import polypheny
connection = polypheny.connect(
(os.getenv("POLYPHENY_HOST", "127.0.0.1"), int(os.getenv("POLYPHENY_PORT", "20590"))),
username=os.getenv("POLYPHENY_USER", "pa"),
password=os.getenv("POLYPHENY_PASSWORD", ""),
transport="plain",
)
table = f"py_mql_{uuid4().hex[:12]}"
cursor = connection.cursor()
try:
cursor.execute(f"CREATE TABLE public.{table} (id INTEGER PRIMARY KEY, label VARCHAR(40))")
cursor.execute(f"INSERT INTO public.{table} VALUES (?, ?)", (1, "Apple"))
connection.commit()
cursor.executeany("mongo", f"db.{table}.find()", namespace="public")
for document in cursor:
print(document["id"], document["label"])
finally:
try:
connection.rollback()
cursor.execute(f"DROP TABLE IF EXISTS public.{table}")
finally:
connection.close()
See the cursor reference for fetch sizes, named parameters and metadata about relational result columns.