Skip to main content

A python DolphinDB API based on DBAPI2.0 Specification

Project description

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}

Related Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydolphindb-1.1.2-py3-none-any.whl (32.1 kB view details)

Uploaded Python 3

File details

Details for the file pydolphindb-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: pydolphindb-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 32.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for pydolphindb-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0fdb334d14989a8f09b32894cb80dbc0dbcda7e3b1cb41fc41c6f66a728b6e5f
MD5 20b699164cfb7e7c9526878084f16f4b
BLAKE2b-256 d5d290a2c2696eeeca81890d4ded6a12ceb345d7cdd48336212dae85734bcf8a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page