Quick Start¶
Get started with sqlalchemy-pyaltibase to use Altibase through SQLAlchemy Core or ORM.
Requirements¶
Before you begin
- Python 3.10+
- SQLAlchemy 2.x
- Altibase server reachable from your application host
pyaltibasedriver installed (directly or via extras)
Install¶
Optional extra to install the DB-API driver with the dialect package:
Connection URL¶
Use the Altibase SQLAlchemy URL form:
Defaults used by the dialect when URL parts are omitted:
host:localhostport:20300user:sysdatabase: empty string ("")
SQLAlchemy Core example¶
from sqlalchemy import Column, Integer, MetaData, String, Table, create_engine, select
engine = create_engine("altibase://sys:password@localhost:20300/mydb")
metadata = MetaData()
users = Table(
"users",
metadata,
Column("id", Integer, primary_key=True, autoincrement=True),
Column("name", String(100), nullable=False),
)
metadata.create_all(engine)
with engine.begin() as conn:
conn.execute(users.insert().values(name="alice"))
with engine.connect() as conn:
rows = conn.execute(select(users.c.id, users.c.name)).all()
print(rows)
SQLAlchemy ORM example¶
from sqlalchemy import Integer, String, create_engine, select
from sqlalchemy.orm import DeclarativeBase, Mapped, Session, mapped_column
class Base(DeclarativeBase):
pass
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
name: Mapped[str] = mapped_column(String(100), nullable=False)
engine = create_engine("altibase://sys:password@localhost:20300/mydb")
Base.metadata.create_all(engine)
with Session(engine) as session:
session.add(User(name="alice"))
session.commit()
with Session(engine) as session:
users = session.scalars(select(User)).all()
print(users)
Async example¶
Install with async support:
import asyncio
from sqlalchemy import text
from sqlalchemy.ext.asyncio import create_async_engine
async def main():
engine = create_async_engine("altibase+aioodbc://sys:password@localhost:20300/mydb")
async with engine.begin() as conn:
result = await conn.execute(text("SELECT 1 FROM DUAL"))
print(result.scalar())
await engine.dispose()
asyncio.run(main())
Runtime architecture¶
mermaid
flowchart LR
app[Application] --> sa[SQLAlchemy Core/ORM]
sa --> dialect[sqlalchemy-pyaltibase\nAltibaseDialect]
dialect --> dbapi[pyaltibase DB-API]
dbapi --> db[Altibase]
Autoincrement behavior differs from some databases
For autoincrement integer primary keys, this dialect creates and drops implicit sequences via table event listeners. See Dialect Features.