Python连接达梦数据库
Python连接达梦数据库
1、安装dmPython(windows环境)
01.安装DM数据库软件并设置DM_HOME环境变量 ##只需要DM数据库客户端就可以远程连接达梦数据库
dmPython源码依赖DM安装目录中提供的include头文件,编译安装前需要检查是否安装DM数据库软件,并设置DM_HOME环境变量。
DM_HOME=C:\Program Files\dmdbms
02.安装python
双击安装包python-3.7.8-amd64
Install launcher for all users(recommended) > Customize install > 全部选项 > Next > 不选择最后两项Download > 安装路径C:\Program Files\Python37 > Install > Close
03.测试Python
C:\Users\Administrator>python
Python 3.7.8 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
03.安装编译工具Microsoft Visual C++ Build Tools
双击安装包vs_community__1266218921.1639064518
选上"使用C++的桌面开发"和"Python开发"
04.编译安装dmPython
安装完DM数据库软件后,在安装路径下的drivers目录下,找到dmPython的驱动源码。
进入到dmPython驱动源码目录
C:\Program Files>cd C:\Program Files\dmdbms\drivers\python\dmPython
C:\Program Files\dmdbms\drivers\python\dmPython>python setup.py install
Installed c:\program files\python37\lib\site-packages\dmpython-2.3-py3.7-win-amd64.egg
Processing dependencies for dmPython=2.3
Finished processing dependencies for dmPython=2.3
编译安装结束后使用pip list命令查看是否安装成功
C:\Program Files\dmdbms\drivers\python\dmPython>pip list
Package Version
dmpython 2.3
pip 20.1.1
setuptools 47.1.0
05.查看搜索路径并将dpi目录文件拷贝到搜索路径下
dmPython通过调用DM DPI接口完成Python模块扩展。在其使用过程中,除Python标准库以外,还需要DPI的运行环境。
06.进入python解释器查看搜索路径
C:\Program Files\dmdbms\drivers\python\dmPython>python
Python 3.7.8 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
import sys
sys.path
[’’, ‘C:\Program Files\Python37\python37.zip’, ‘C:\Program Files\Python37\DLLs’, ‘C:\Program Files\Python37\lib’, ‘C:\Program Files\Python37’, ‘C:\Program Files\Python37\lib\site-packages’, ‘C:\Program Files\Python37\lib\site-packages\dmpython-2.3-py3.7-win-amd64.egg’]
07.把达梦安装包的dpi目录里面的文件全部拷贝到搜索路径的最后一个目录
C:\Program Files\dmdbms\drivers\dpi下的所有文件拷贝到C:\Program Files\Python37\lib\site-packages\dmpython-2.3-py3.7-win-amd64.egg
08.编写测试代码
C:\Program Files\dmdbms\drivers\python\dmPython>python
import dmPython
conn=dmPython.connect(user=‘SYSDBA’,password=‘SYSDBA’,server= ‘192.168.1.31’,port=5236)
cursor = conn.cursor()
cursor.execute(‘select * from test’)
values = cursor.fetchall()
print(values)
cursor.close()
conn.close()
09.输出数据库中的用户名则表示连接数据库成功
C:\Program Files\dmdbms\drivers\python\dmPython>python
Python 3.7.8 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
import dmPython
conn=dmPython.connect(user=‘SYSDBA’,password=‘SYSDBA’,server= ‘192.168.1.31’,port=5236)
cursor = conn.cursor()
cursor.execute(‘select * from test’)
<builtins.DmdbCursor on <dmPython.Connection to SYSDBA@192.168.1.31:5236>>
values = cursor.fetchall()
print(values)
[(1, ‘JDBC插入数据测试1’), (2, ‘JDBC插入数据测试2’)]
cursor.close()
conn.close()
2、安装dmPython(Linux环境)
01.安装DM数据库软件并设置DM_HOME环境变量
dmPython源码依赖DM安装目录中提供的include头文件,编译安装前需要检查是否安装DM数据库软件,并设置DM_HOME环境变量。
设置DM_HOME环境变量:
vi /root/.bash_profile
export DM_HOME=/home/dmdba/dmdbms
source /root/.bash_profile
02.安装编译工具gcc
yum install gcc
[root@KylinV10-02 ~]# gcc --version
gcc (GCC) 7.3.0
Copyright © 2017 Free Software Foundation, Inc.
03.确认当前使用的Python版本是自己编译安装的还是系统自带或通过yum源方式安装
如果是自己编译安装的Python则不需要安装python3-devel。
如果是系统自带或通过yum源方式安装的Python,需要安装python3-devel依赖。
mkdir -p /dmiso
mount /dev/sr0 /dmiso
cd /dmiso/Packages/
rpm -ivh python3-devel-3.7.9-6.ky10.x86_64.rpm
04.编译安装dmPython
安装完DM数据库软件后,在安装路径下的drivers目录下,找到dmPython的驱动源码。
进入到dmPython驱动源码目录
cd /home/dmdba/dmdbms/drivers/python/dmPython
编译安装dmPython
python3 setup.py install
Installed /usr/local/lib64/python3.7/site-packages/dmPython-2.3-py3.7-linux-x86_64.egg
Processing dependencies for dmPython=2.3
Finished processing dependencies for dmPython=2.3
05.编译安装结束后使用pip3 list命令查看是否安装成功 ##麒麟V10这条命令执行无结果,需要安装pip
tar -zxvf /opt/pip-18.1.tar.gz -C /usr/lib/python3.7/site-packages/
cd /usr/lib/python3.7/site-packages/pip-18.1/
python setup.py install
Package Version
dmPython 2.3
pip 18.1
setuptools 44.1.1
06.设置LD_LIBRARY_PATH环境变量
dmPython通过调用DM DPI接口完成Python模块扩展。在其使用过程中,除Python标准库以外,还需要DPI的运行环境。
vi /root/.bash_profile
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/dmdba/dmdbms/drivers/dpi/
source /root/.bash_profile
06.编写测试代码
[root@kylinV10-01 dpi]# python3
import dmPython
conn=dmPython.connect(user=‘SYSDBA’,password=‘SYSDBA’,server=‘192.168.1.31’,port=5236)
cursor = conn.cursor()
cursor.execute(‘select * from test’)
values = cursor.fetchall()
print(values)
cursor.close()
conn.close()
07.输出数据库中的用户名则表示连接数据库成功
[root@kylinV10-01 dpi]# 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=‘SYSDBA’,password=‘SYDBA’,server=‘192.168.1.31’,port=5236)
cursor = conn.cursor()
cursor.execute(‘select * from test’)
<builtins.DmdbCursor on <dmPython.Connection to SYSDBA@192.168.1.31:5236>>
values = cursor.fetchall()
print(values)
[(1, ‘JDBC插入数据测试1’), (2, ‘JDBC插入数据测试2’)]
cursor.close()
conn.close()
达梦数据库安装参考:
https://blog.csdn.net/qq_42079557/article/details/121329833?spm=1001.2014.3001.5501
达梦社区更多案例
https://eco.dameng.com
更多推荐
所有评论(0)