Oracle Async Connection¶
Use SQLSpec’s async Oracle adapter to fetch the current timestamp. Override credentials with
SQLSPEC_ORACLE_USER, SQLSPEC_ORACLE_PASSWORD, and SQLSPEC_ORACLE_DSN.
SQLSPEC_ORACLE_USER=system SQLSPEC_ORACLE_PASSWORD=oracle SQLSPEC_ORACLE_DSN=localhost/FREE \
uv run python docs/examples/adapters/oracledb/connect_async.py
Source¶
1"""Async Oracle connection powered by SQLSpec."""
2
3import asyncio
4import os
5
6from sqlspec.adapters.oracledb import OracleAsyncConfig
7
8__all__ = ("main",)
9
10
11USER = os.getenv("SQLSPEC_ORACLE_USER", "system")
12PASSWORD = os.getenv("SQLSPEC_ORACLE_PASSWORD", "oracle")
13DSN = os.getenv("SQLSPEC_ORACLE_DSN", "localhost/FREE")
14config = OracleAsyncConfig(
15 bind_key="docs_oracle_async", pool_config={"user": USER, "password": PASSWORD, "dsn": DSN, "min": 1, "max": 4}
16)
17
18
19async def main() -> None:
20 """Connect to Oracle and print the current timestamp."""
21 async with config.provide_session() as session:
22 result = await session.select_one_or_none("SELECT systimestamp AS ts FROM dual")
23 if result:
24 print({"adapter": "oracledb", "timestamp": str(result["ts"])})
25 else:
26 print({"adapter": "oracledb", "timestamp": "unknown"})
27
28
29if __name__ == "__main__":
30 asyncio.run(main())