{"templateId":"markdown","sharedDataIds":{"sidebar":"sidebar-sidebars.yaml"},"props":{"metadata":{"markdoc":{"tagList":[]},"redocly_category":"Guides","type":"markdown"},"seo":{"title":"Working with DB-API","description":"Treasure Data Product Documentation · Collect and Unify · Segment and Activate · Experiment and Analyze · Decisioning Automate with AI Scale and Trust.","siteUrl":"https://docs.treasuredata.com","lang":"en-US","llmstxt":{"hide":false,"sections":[{"title":"Table of contents","includeFiles":["**/*"],"excludeFiles":[]}],"excludeFiles":[]}},"dynamicMarkdocComponents":[],"compilationErrors":[],"ast":{"$$mdtype":"Tag","name":"article","attributes":{},"children":[{"$$mdtype":"Tag","name":"Heading","attributes":{"level":1,"id":"working-with-db-api","__idx":0},"children":["Working with DB-API"]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"intro","__idx":1},"children":["Intro"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["pytd"]}," implements ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://www.python.org/dev/peps/pep-0249/"},"children":["Python Database API Specification v2.0"]}," with the help of ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://github.com/prestodb/presto-python-client"},"children":["prestodb/presto-python-client"]},"."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"setup","__idx":2},"children":["Setup"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["To connect to the API declare a ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["connect"]}," object. The default engine is Trino(Presto). You can manually specify Hive via the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["default_engine"]}," argument."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"python","header":{"controls":{"copy":{}}},"source":"import pytd\nfrom pytd.dbapi import connect\n\n\nconn = connect(pytd.Client(database='sample_datasets'))\n\n# or, connect with Hive:\nconn = connect(pytd.Client(database='sample_datasets', default_engine='hive'))\n","lang":"python"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"query","__idx":3},"children":["Query"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["cursor"]}," function allows you to flexibly fetch query results from a custom function:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"python","header":{"controls":{"copy":{}}},"source":"def query(sql, connection):\n    cur = connection.cursor()\n    cur.execute(sql)\n    rows = cur.fetchall()\n    columns = [desc[0] for desc in cur.description]\n    return {'data': rows, 'columns': columns}\n\nquery('select symbol, count(1) as cnt from nasdaq group by 1 order by 1', conn)\n","lang":"python"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"example","__idx":4},"children":["Example"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Below is an example of generator-based iterative retrieval, just like ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.iterrows.html"},"children":["pandas.DataFrame.iterrows"]},":"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"python","header":{"controls":{"copy":{}}},"source":"def iterrows(sql, connection):\n    cur = connection.cursor()\n    cur.execute(sql)\n    index = 0\n    columns = None\n    while True:\n        row = cur.fetchone()\n        if row is None:\n            break\n        if columns is None:\n            columns = [desc[0] for desc in cur.description]\n        yield index, dict(zip(columns, row))\n        index += 1\n\nfor index, row in iterrows('select symbol, count(1) as cnt from nasdaq group by 1 order by 1', conn):\n    print(index, row)\n# 0 {'cnt': 590, 'symbol': 'AAIT'}\n# 1 {'cnt': 82, 'symbol': 'AAL'}\n# 2 {'cnt': 9252, 'symbol': 'AAME'}\n# 3 {'cnt': 253, 'symbol': 'AAOI'}\n# 4 {'cnt': 5980, 'symbol': 'AAON'}\n# ...\n","lang":"python"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"further-reading","__idx":5},"children":["Further Reading"]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://pytd-doc.readthedocs.io/en/latest/dbapi.html"},"children":["pytd DB-API Reference"]}]}]}]},"headings":[{"value":"Working with DB-API","id":"working-with-db-api","depth":1},{"value":"Intro","id":"intro","depth":2},{"value":"Setup","id":"setup","depth":2},{"value":"Query","id":"query","depth":2},{"value":"Example","id":"example","depth":2},{"value":"Further Reading","id":"further-reading","depth":2}],"frontmatter":{"seo":{"title":"Working with DB-API"}},"lastModified":"2026-02-17T01:46:37.000Z","pagePropGetterError":{"message":"","name":""}},"slug":"/tools/pytd/working-with-db-api","userData":{"isAuthenticated":false,"teams":["anonymous"]},"isPublic":true}