Dameng Database Python Interface
dmPython is the official Python interface to the Dameng database, designed to facilitate connections and operations following the Python DB API 2.0 specification. It enables Python applications to directly interact with Dameng databases. The library is actively maintained by Dameng Database and currently stands at version 2.5.32, with frequent updates addressing bug fixes and new features.
Common errors
-
dmPython.DatabaseError: [CODE:-70089]加密模块加载失败
cause The system's SSL encryption module does not meet the requirements, or the dmPython driver cannot locate the necessary SSL libraries.fixUpdate `libssl` to version 1.1 or higher. Alternatively, set the `LD_LIBRARY_PATH` (Linux) or `PATH` (Windows) environment variable to point to the `dmssl` directory located within the `dmPython` installation (e.g., `export LD_LIBRARY_PATH=/usr/local/lib/python3.13/site-packages/dmssl`). -
pip install dmpython==2.4.5 fails
cause Specific older versions of dmPython, such as 2.4.5, have been removed from PyPI.fixInstall the latest stable version of dmPython using `pip install dmpython` or specify a currently available version on PyPI.
Warnings
- gotcha When installing dmPython from source (not via `pip install`), the Dameng Database software must be installed and the `DM_HOME` environment variable must be correctly set, as the source build depends on include headers from the DM installation directory.
- breaking Older versions of `dmPython` (e.g., prior to 2.5.30) had known issues with multi-threaded concurrent connection release, which could lead to application instability. Similarly, versions before 2.5.26 experienced interruptions when using `executemany` with character data containing unidentifiable characters for the current encoding.
- gotcha Be aware that there is another distinct Python library named 'DM_Control_Python' which is used for controlling Damiao motors via CAN. Ensure you are importing and using 'dmPython' for Dameng database connectivity to avoid confusion and unexpected errors.
Install
-
pip install dmpython
Imports
- dmPython
import dmPython
Quickstart
import dmPython
import os
# Replace with your actual Dameng database connection details
user = os.environ.get('DM_USER', 'SYSDBA')
password = os.environ.get('DM_PASSWORD', 'Dameng123')
host = os.environ.get('DM_HOST', 'localhost')
port = os.environ.get('DM_PORT', '5236')
schema = os.environ.get('DM_SCHEMA', 'SCH1')
connection_string = f'{user}/{password}@{host}:{port}/{schema}'
try:
conn = dmPython.connect(connection_string)
cursor = conn.cursor()
cursor.execute("SELECT SYSDATE FROM DUAL")
result = cursor.fetchone()
print(f"Current database time: {result[0]}")
cursor.close()
conn.close()
except dmPython.DatabaseError as e:
print(f"Database connection or query error: {e}")