Develop Python connection to Damon database (DM8)

Keywords: Python Database

reference
https://eco.dameng.com/docs/zh-cn/app-dev/python-python.html
https://eco.dameng.com/docs/zh-cn/start/python-development.html

outline

DMPython

dmPython is a database access interface provided by DM and developed according to the API usage regulations in Python DB API version 2.0
dmPython implements these API s to enable Python applications to access DM databases

My understanding is:
dmPython is the third-party library of python, which provides some modules and methods to access DM database (for example, python program can connect DM database by calling the method dmPython.connect)

DM DPI

dmPython completes python module extension by calling DM DPI interface
In addition to the Python standard library, the running environment of DPI is also required

My understanding is:
Compared with DM DPI, dmPython is more advanced and abstract (it is an interface designed for Python, similar to DM PHP of PHP); DM DPI is responsible for implementing some lower level modules or methods (that is, I think DM DPI is an interface designed for DM database), so dmPython needs to call DM DPI

[to sum up]
In my opinion, the interaction between PHP program and DM database = PHP program + dmPython + DM DPI + DM database

Environment introduction

Softwareedition
DM databaseDM8
PythonPython 3.7.9

Install DM8 database

reference resources:
https://eco.dameng.com/docs/zh-cn/start/install-dm-linux-prepare.html
https://blog.csdn.net/weixin_41709724/article/details/121101353

Install Python

  • Steps for installing Python (omitted temporarily)
Python The version is as follows
[root@dw1 dpi]# python3 --version
Python 3.7.9
  • Compile and install dmPython

Check the compiler gcc (if not, install it)

[root@dw1 dpi]# rpm -qa|grep gcc
libgcc-7.3.0-20190804.35.p02.ky10.x86_64
gcc-7.3.0-20190804.35.p02.ky10.x86_64

Check whether there is Python 3-devel (if not, install it)

[root@dw1 dpi]# rpm -qa|grep python3-devel
python3-devel-3.7.9-6.ky10.x86_64

After installing the DM database software, find the driver source code of dmPython in the drivers directory under the installation path

[root@dw1 python]# cd /home/dmdba/dmdbms/drivers/python
[root@dw1 python]# ls
django155  django196  django223  django317  dmPython  sqlalchemy

Enter the dmPython driver source directory

[root@dw1 python]# cd dmPython/
[root@dw1 dmPython]# pwd
/home/dmdba/dmdbms/drivers/python/dmPython

Application scenario of build tool setup.py
https://www.cnblogs.com/maociping/p/6633948.html

Compile and install the python package dmPython and install it into the specified Python library

[root@dw1 dmPython]# python3 setup.py install

After compiling and installing, dmPython Installed in the following directory
[root@dw1 dmPython-2.3-py3.7-linux-x86_64.egg]# pwd
/usr/local/lib64/python3.7/site-packages/dmPython-2.3-py3.7-linux-x86_64.egg

You can see the generation dmPython.py file
[root@dw1 dmPython-2.3-py3.7-linux-x86_64.egg]# ll dmPython.py
-rw-r--r-- 1 root root 313 11 June 30-15:24 dmPython.py

[Note 1]
The source code of dmPython depends on the include header file provided in the DM installation directory. Before compiling and installing, you need to check whether the DM database software is installed and set DM_HOME environment variable.

Here, run the Python program directly on the server where the DM database is installed

  • Check dmPython installation

After compilation and installation, use the pip3 list command to check whether the installation is successful

[root@dw1 dmPython]# pip3 list | grep dmPython
dmPython               2.3
  • Set LD_LIBRARY_PATH environment variable

dmPython completes Python module extension by calling DM DPI interface. In addition to the python standard library, the running environment of DPI is also required.

Configure environment variables LD_LIBRARY_PATH,Easy to call libdmdpi.so You can find it
vi /root/.bash_profile
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/dmdba/dmdbms/drivers/dpi/
source /root/.bash_profile

Interact with DM database using Python

Interact with the database through Python interactive programming

[Note 1]
The IP, port, user name and password of the database shall be modified according to the actual situation

Python returns the same result as the disql query (SYSSSO, SYSDBA, SYS, SYSAUDITOR)

SQL> select username from dba_users;

Line number     username  
---------- ----------
1          SYSSSO
2          SYSDBA
3          SYS
4          SYSAUDITOR

Elapsed time: 4.973(millisecond). Execution number:1200.

The python code is as follows:

python3 
import dmPython
conn=dmPython.connect(user='SYSDBA',password='SYSDBA',server='192.168.8.10', port=5236)
cursor = conn.cursor()
cursor.execute('select username from dba_users')
values = cursor.fetchall()
print(values)
cursor.close()
conn.close()

Damon cloud adaptation Center:
https://eco.dameng.com

Posted by Ruud Hermans on Thu, 02 Dec 2021 14:39:58 -0800