1 - 安装 DM 数据库软件和设置 DM_HOME 环境变量
dmPython 源码依赖 DM 安装目录中提供的 include 头文件,编译安装前需要检查是否安装 DM 数据库软件,并设置 DM_HOME 环境变量。
可访问达梦云适配中心下载试用,下载 DM8 数据库试用版并安装,请参考 DM 数据库安装。
设置 DM_HOME 环境变量:
vi /root/.bash_profile
export DM_HOME=/home/dmdba/dmdbms
source /root/.bash_profile
2 - 安装编译工具 gcc
yum install gcc
3 - 确认当前使用的 Python 版本是自己编译安装的还是系统自带或通过 yum 源方式安装
如果是自己编译安装的 Python 则不需要安装 python3-devel。
如果是系统自带或通过 yum 源方式安装的 Python,需要安装 python3-devel 依赖。
使用OS 盘安装。
[root@test Packages]# rpm -ivh python3-devel-3.7.9-6.ky10.x86_64.rpm
4 - 编译并安装 dmPython
安装完 DM 数据库软件后,在安装路径下的 drivers 目录下,找到 dmPython 的驱动源码。
[root@test python]# pwd
/home/dmdba/dmdbms/drivers/python
[root@test python]# ll
总用量 1124
drwxr-xr-x 2 dmdba dinstall 50 11月 26 11:48 django-comment-migrate
-rwxr-xr-x 1 dmdba dinstall 1145167 10月 13 16:46 'DM8 - dmPython.pdf'
drwxr-xr-x 4 dmdba dinstall 44 11月 26 11:48 dmDjango
drwxr-xr-x 5 dmdba dinstall 4096 11月 26 20:58 dmPython
drwxr-xr-x 5 dmdba dinstall 75 11月 26 11:48 dmSQLAlchemy
进入到 dmPython 驱动源码目录
cd home/dmdba/dmdbms/drivers/python/dmPython
编译安装 dmPython
python3 setup.py install
输出如下:
[dmdba@test ~]$ cd /dm/dmdbms/drivers/python/dmPython/
[dmdba@test dmPython]$ python3 setup.py install
running install
running bdist_egg
running egg_info
writing dmPython.egg-info/PKG-INFO
writing dependency_links to dmPython.egg-info/dependency_links.txt
writing top-level names to dmPython.egg-info/top_level.txt
reading manifest file 'dmPython.egg-info/SOURCES.txt'
writing manifest file 'dmPython.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_ext
creating build/bdist.linux-x86_64/egg
copying build/lib.linux-x86_64-3.7/dmPython.cpython-37m-x86_64-linux-gnu.so -> build/bdist.linux-x86_64/egg
creating stub loader for dmPython.cpython-37m-x86_64-linux-gnu.so
byte-compiling build/bdist.linux-x86_64/egg/dmPython.py to dmPython.cpython-37.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying dmPython.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying dmPython.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying dmPython.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying dmPython.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
writing build/bdist.linux-x86_64/egg/EGG-INFO/native_libs.txt
zip_safe flag not set; analyzing archive contents...
__pycache__.dmPython.cpython-37: module references __file__
creating 'dist/dmPython-2.5.22-py3.7-linux-x86_64.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing dmPython-2.5.22-py3.7-linux-x86_64.egg
creating /usr/local/lib64/python3.7/site-packages/dmPython-2.5.22-py3.7-linux-x86_64.egg
Extracting dmPython-2.5.22-py3.7-linux-x86_64.egg to /usr/local/lib64/python3.7/site-packages
Adding dmPython 2.5.22 to easy-install.pth file
Installed /usr/local/lib64/python3.7/site-packages/dmPython-2.5.22-py3.7-linux-x86_64.egg
Processing dependencies for dmPython==2.5.22
Finished processing dependencies for dmPython==2.5.22
5 - 设置 LD_LIBRARY_PATH 环境变量
dmPython 通过调用 DM DPI 接口完成 Python 模块扩展。在其使用过程中,除 Python 标准库以外,还需要 DPI 的运行环境。
vi /root/.bash_profile
export DM_HOME="/home/dmdba/dmdbms"
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/dmdba/dmdbms/drivers/dpi/
source /root/.bash_profile
6 - 编写的测试代码
import dmPython
conn=dmPython.connect(user='xx',password='xx',server= 'xx',port=5236)
cursor = conn.cursor()
cursor.execute('select username from dba_users')
values = cursor.fetchall()
print(values)
cursor.close()
conn.close()
注意:密码'Admin@123' 无需再转义。
输出类似:
[root@test ~]# python3
Python 3.7.9 (default, Mar 2 2021, 02:43:11)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dmPython
>>> conn=dmPython.connect(user='xx',password='xx',server= 'xx',port=5236)
>>> cursor = conn.cursor()
>>> cursor.execute('select username from dba_users')
<builtins.DmdbCursor on <dmPython.Connection to sysdba@192.168.56.166:5236>>
>>> values = cursor.fetchall()
>>> print(values)
[('SYS',), ('SYSDBA',), ('SYSSSO',), ('SYSAUDITOR',)]
>>> cursor.close()
>>> conn.close()
输出数据库中的用户名则表示连接数据库成功。
欢迎访问达梦技术分享社区 ECO
https://eco.dameng.com