Skip to main content

pydolphindb

pydolphindb is a library that enables you to connect to the DolphinDB server in Python. It implements the classes and methods as defined in the DolphinDB Python API specification and is compliant with the Python Database API v2.0 specification (PEP-249). pydolphindb contains a Python DolphinDB client library and supports connecting to DolphinDB using SQLAlchemy.

1. Installing pydolphindb

1.1 Prerequisites

Make sure you use:

  • Python 3.7 or higher
  • DolphinDB 1.30.21 or higher can also 2.00.9 or higher

Install the following libraries:

  • dolphindb 3.0.2.3 or higher
  • sqlalchemy 1.4 or higher while lower than 2.0

1.2 Installation

Execute the following pip command:

pip install pydolphindb

2. Functions

2.1 Function Reference

Function Parameters Description
pydolphindb.connect(host, port username, password) host (string) - hostname of the server to connect to.port (integer) - port number of the server to connect to.username (string) - username for login.password (string) - password for login. Connect to database.
connection.cursor() - Create a cursor object.
cursor.execute(script,Parameters = None) script (string) - script to execute.parameters (tuple, list or dictionary) - Optional. Parameters used with script. Execute script.If parameters is a list or tuple, use %s as a placeholder in the script. If parameters is a dict, use %(name)s as a placeholder in the script.
cursor.executemany(script,seq_of_parameters) script (string) - script to execute.seq_of_parameters (list or tuple) - a sequence (list or tuple) of lists or tuples used with script. Execute the script against all parameter sequences specified in seq_of_parameters. Use this function for a batch insert operation.
cursor.fetchone() - Fetch the next record.
cursor.fetchmany(size) size (integer) - number of records to fetch. Fetch multiple (as specified by size) records.
cursor.fetchall() - Fetch all records.

Note: pydolphindb does not support functions to commit or rollback a transaction.

2.2 Sample Code

In this example, we import pydolphindb with the import command, then connect to DolphinDB with the connect function. Functions execute and executemany are called to create and write to a database, respectively, through the specified DolphinDB script. Functions fetchone, fetchmany, and fetchall are called to fetch data from the database. The connection is closed with the close function.

import pydolphindb

# connect to database server
db = pydolphindb.connect(host='localhost',
                         port=8848,
                         username='admin',
                         password='123456')

# create a cursor object
cursor = db.cursor()

# DolphinDB script for creating a database
script = """
    t = table(100:0, `id`time`vol, [SYMBOL, DATE, INT])
"""

# execute DolphinDB script with execute()
cursor.execute(script)

# execute DolphinDB script with executemany()
cursor.executemany('insert into t values(`%s, %s, %d)', [
    ("APPL", "2012.01.02", 12), ("IBM", "2012.01.03", 16),
    ("APPL", "2012.01.03", 20), ("AMZN", "2012.01.03", 100),
])

# get the next record with fetchone()
data = cursor.fetchone()

# the result is None
print("Result:", data)

cursor.execute("select * from t")

# get the next two records with fetchmany()
data = cursor.fetchmany(size=2)
print("Result: ", data)

# get all records with fetchall()
data = cursor.fetchall()
print("Result: ", data)

# close connection
db.close()

3. Connecting to DolphinDB Using SQLAlchemy

SQLAlchemy is a Python SQL toolkit and Object Relational Mapper which is designed for efficient database access. pydolphindb is a Python DB API 2.0 (PEP 249) client for DolphinDB. The DolphinDB SQLAlchemy dialect is supported so you can connect to the DolphinDB databases through a SQLAlchemy engine.

The following script shows how to connect to and interact with DolphinDB through SQLAlchemy in Python:

from sqlalchemy import create_engine

# create an engine for connection
engine = create_engine("dolphindb://admin:123456@localhost:8848")

# DolphinDB script for a database query
script = """
    t = table([1, 2, 3] as a, ["a", "b", "c"] as b)
    select * from t
"""

# run script with execute()
result = engine.execute(script)

# print all results
print(result.all())

Note:

  • The DolphinDB SQLAlchemy currently does not support ORM (object-relational mapping).
  • When creating engine, pass the DolphinDB database URL to create_engine as a string in the following pattern: dolphindb://{user}:{password}@{host}:{port}

Metadata

Release files for pydolphindb 1.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for pydolphindb 1.1.2
File Interpreter ABI Platform Actions
pydolphindb-1.1.2-py3-none-any.whl Python 3 none any Details for pydolphindb-1.1.2-py3-none-any.whl

Release files / pydolphindb-1.1.2-py3-none-any.whl

Download URL pydolphindb-1.1.2-py3-none-any.whl
Size 32.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0fdb334d14989a8f09b32894cb80dbc0dbcda7e3b1cb41fc41c6f66a728b6e5f
BLAKE2b-256 checksum
How to use checksums
d5d290a2c2696eeeca81890d4ded6a12ceb345d7cdd48336212dae85734bcf8a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.16

Security for pydolphindb 1.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page