{"library":"trino","code":"import os\nfrom trino.dbapi import connect\nfrom trino.auth import BasicAuthentication\n\nTRINO_HOST = os.environ.get('TRINO_HOST', 'localhost')\nTRINO_PORT = int(os.environ.get('TRINO_PORT', '8080'))\nTRINO_USER = os.environ.get('TRINO_USER', 'your_user')\nTRINO_CATALOG = os.environ.get('TRINO_CATALOG', 'system')\nTRINO_SCHEMA = os.environ.get('TRINO_SCHEMA', 'runtime')\nTRINO_PASSWORD = os.environ.get('TRINO_PASSWORD') # Optional, for BasicAuth\nTRINO_HTTP_SCHEME = os.environ.get('TRINO_HTTP_SCHEME', 'http')\n\n\nauth = None\nif TRINO_PASSWORD:\n    auth = BasicAuthentication(TRINO_USER, TRINO_PASSWORD)\n\ntry:\n    with connect(\n        host=TRINO_HOST,\n        port=TRINO_PORT,\n        user=TRINO_USER,\n        catalog=TRINO_CATALOG,\n        schema=TRINO_SCHEMA,\n        http_scheme=TRINO_HTTP_SCHEME,\n        auth=auth # Pass auth if not None\n    ) as conn:\n        with conn.cursor() as cur:\n            cur.execute('SELECT node_id, state FROM system.runtime.nodes')\n            rows = cur.fetchall()\n            for row in rows:\n                print(row)\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\n    print(\"Please ensure a Trino server is running and accessible at the specified host and port.\")\n    print(\"For secure connections, ensure 'TRINO_HTTP_SCHEME' is 'https' and provide authentication details.\")","lang":"python","description":"This quickstart demonstrates how to connect to a Trino server using the DBAPI interface, execute a simple query, and fetch results. It uses environment variables for connection parameters for flexibility and basic authentication if a password is provided. The `with` statement ensures proper resource management for both connection and cursor.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":0}]}