ploomber.clients.DBAPIClient

class ploomber.clients.DBAPIClient(connect_fn, connect_kwargs, split_source=None)

A client for a PEP 249 compliant client library

Parameters
  • connect_fn (callable) – The function to use to open the connection

  • connect_kwargs (dict) – Keyword arguments to pass to connect_fn

  • split_source (str, optional) – Some database drivers do not support multiple commands in a single execute statement. Use this optiion to split commands by a given character (e.g. ‘;’) and send them one at a time. Defaults to None (no splitting)

Examples

Spec API:

Given the following clients.py:

import sqlite3
from ploomber.clients import DBAPIClient

def get():
    return DBAPIClient(sqlite3.connect, dict(database='my.db'))

Spec API (dag-level client):

clients:
    # key is a task class such as SQLDump or SQLScript
    SQLDump: clients.get

tasks:
    - source: query.sql
      product: output/data.csv

Spec API (task-level client):

tasks:
    - source: query.sql
      product: output/data.csv
      client: clients.get

Python API (dag-level client):

>>> import sqlite3
>>> import pandas as pd
>>> from ploomber import DAG
>>> from ploomber.products import File
>>> from ploomber.tasks import SQLDump
>>> from ploomber.clients import DBAPIClient
>>> con_raw = sqlite3.connect(database='my.db')
>>> df = pd.DataFrame({'a': range(100), 'b': range(100)})
>>> _ = df.to_sql('numbers', con_raw, index=False)
>>> con_raw.close()
>>> dag = DAG()
>>> client = DBAPIClient(sqlite3.connect, dict(database='my.db'))
>>> dag.clients[SQLDump] = client # dag-level client
>>> _ = SQLDump('SELECT * FROM numbers', File('data.parquet'),
...             dag=dag, name='dump',
...             client=client,
...             chunksize=None) # no need to pass client here
>>> _ = dag.build()
>>> df = pd.read_parquet('data.parquet')
>>> df.head(3)
   a  b
0  0  0
1  1  1
2  2  2

Python API (task-level client):

>>> import sqlite3
>>> import pandas as pd
>>> from ploomber import DAG
>>> from ploomber.products import File
>>> from ploomber.tasks import SQLDump
>>> from ploomber.clients import DBAPIClient
>>> con_raw = sqlite3.connect(database='some.db')
>>> df = pd.DataFrame({'a': range(100), 'b': range(100)})
>>> _ = df.to_sql('numbers', con_raw, index=False)
>>> con_raw.close()
>>> dag = DAG()
>>> client = DBAPIClient(sqlite3.connect, dict(database='some.db'))
>>> _ = SQLDump('SELECT * FROM numbers', File('data.parquet'),
...             dag=dag, name='dump',
...             client=client,  # pass client to task
...             chunksize=None)
>>> _ = dag.build()
>>> df = pd.read_parquet('data.parquet')
>>> df.head(3)
   a  b
0  0  0
1  1  1
2  2  2

See also

ploomber.clients.SQLAlchemyClient

A client to connect to a database using sqlalchemy as backend

Methods

close()

Close connection if there is an active one

cursor()

execute(code)

Execute code with the existing connection

close()

Close connection if there is an active one

cursor()
execute(code)

Execute code with the existing connection

Attributes

connection

Return a connection, open one if there isn’t any