暂无图片
暂无图片
1
暂无图片
暂无图片
暂无图片

【干货攻略】Python封装DM达梦数据库操作

达梦E学 2021-07-19
6471


dmPython是DM数据库的Python接口,使用dmPython可以实现Python应用程序对达梦数据库的访问和操作。本文将介绍Python封装DM达梦数据库的操作方法。

‍‍‍‍‍‍‍


 ✦ 一、测试环境 ✦ 


1.操作系统版本
    [root@dm8 dmPython]# cat etc/redhat-release
    Red Hat Enterprise Linux Server release 7.5 (Maipo)
    [root@dm8 dmPython]#
    复制

    2、达梦数据库版本:

      [root@dm8 dmPython]# disql SYSDBA/SYSDBA
      服务器[LOCALHOST:5236]:处于普通打开状态
      登录使用时间: 7.944(毫秒)
      disql V8
      SQL> select * from v$version;

      行号 BANNER
      ---------- -------------------------
      1 DM Database Server 64 V8
      2 DB Version: 0x7000a

      已用时间: 1.862(毫秒). 执行号:3186.
      SQL>
      复制

      注意这里的DM8 是测试版,所以版本和官方下载的不一样。


      ☆ 二、安装dmPython ☆


      Python 有现成的mysql,oracle 等数据库包,可以直接import 使用,但对于达梦数据库,必须首先安装dmPython,才可以使用。

      在$DM_HOME/drivers 目录下有多种类型的驱动:

        [dmdba@dm8 ~]$ cd dm/dmdbms/drivers/
        [dmdba@dm8 drivers]$ ls
        dci dpi jdbc node.js oci odbc php_pdo python
        [dmdba@dm8 drivers]$
        [dmdba@dm8 drivers]$ cd python/
        [dmdba@dm8 python]$ ls
        dmPython third
        [dmdba@dm8 python]$ cd dmPython/
        [dmdba@dm8 dmPython]$ ls
        Buffer.c dmPython.vcxproj Error.c exObject.c row.c tObject.c var_pub.h vInterval.c vObject.c
        Buffer.h dmPython.vcxproj.filters Error.h py_Dameng.c row.h trc.c vBfile.c vLob.c vString.c
        Connection.c dmPython.vcxproj.user exBfile.c py_Dameng.h setup.py trc.h vCursor.c vlong.c
        Cursor.c Environment.c exLob.c README.txt strct.h var.c vDateTime.c vNumber.c
        [dmdba@dm8 dmPython]$

        复制

        这里我们需要手工安装dmPython 包。进入dmPython目录后执行命令:

          python setup.py install
          复制

          命令虽简单,但过程插曲比较多。

          1、插曲一:权限问题Permission denied

          开始使用dmdba 用户执行,报如下错误:

            [dmdba@dm8 dmPython]$ python setup.py install
            running install
            error: can't create or remove files in install directory

            The following error occurred while trying to add or remove files in the
            installation directory:

            [Errno 13] Permission denied: '/usr/lib64/python2.7/site-packages/test-easy-install-3032.write-test'

            The installation directory you specified (via --install-dir, --prefix, or
            the distutils default setting) was:

            usr/lib64/python2.7/site-packages/

            Perhaps your account does not have write access to this directory? If the
            installation directory is a system-owned directory, you may need to sign in
            as the administrator or "root" account. If you do not have administrative
            access to this machine, you may wish to choose a different installation
            directory, preferably one that is listed in your PYTHONPATH environment
            variable.

            For information on other options, you may wish to consult the
            documentation at:

            https://pythonhosted.org/setuptools/easy_install.html

            Please make the appropriate changes for your system and try again.

            [dmdba@dm8 dmPython]$
            复制

            根据提示,需要使用root用户来安装dmPython。


            2、 插曲二: cannot locate an Dameng software installation

            切换成root用户执行,又报如下错误:

              [root@dm8 ~]# cd dm/dmdbms/drivers/python/dmPython
              [root@dm8 dmPython]# python setup.py install
              Traceback (most recent call last):
              File "setup.py", line 103, in <module>
              raise DistutilsSetupError("cannot locate an Dameng software "
              distutils.errors.DistutilsSetupError: cannot locate an Dameng software installation
              [root@dm8 dmPython]#
              复制

              因为安装依赖DM的环境,这里在root用户下配置dm的环境变量。

                [root@dm8 dmPython]# cat ~/.bash_profile
                # .bash_profile

                # Get the aliases and functions
                if [ -f ~/.bashrc ]; then
                . ~/.bashrc
                fi

                # User specific environment and startup programs

                PATH=$PATH:$HOME/bin

                export PATH

                export PATH="/dm/dmdbms/bin:$PATH"

                export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/dm/dmdbms/bin"
                export DM_HOME="/dm/dmdbms"
                [root@dm8 dmPython]#
                复制


                3 、插曲三:unable to execute gcc: No such file or directory

                决上面的问题后继续安装:

                  [root@dm8 dmPython]# source ~/.bash_profile
                  [root@dm8 dmPython]# python setup.py install
                  running install
                  running bdist_egg
                  running egg_info
                  creating dmPython.egg-info
                  writing dmPython.egg-info/PKG-INFO
                  writing top-level names to dmPython.egg-info/top_level.txt
                  writing dependency_links to dmPython.egg-info/dependency_links.txt
                  writing manifest file 'dmPython.egg-info/SOURCES.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
                  building 'dmPython' extension
                  creating build
                  creating build/temp.linux-x86_64-2.7
                  gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -DDM64 -I/dm/dmdbms/include -I/usr/include/python2.7 -c py_Dameng.c -o build/temp.linux-x86_64-2.7/py_Dameng.o -DBUILD_VERSION=2.3
                  unable to execute gcc: No such file or directory
                  error: command 'gcc' failed with exit status 1
                  [root@dm8 dmPython]#
                  复制

                  提示缺少gcc 包,直接yum 安装:

                    [root@dm8 dmPython]# yum install gcc* -y
                    复制


                    4、插曲四:py_Dameng.h:8:20: fatal error: Python.h: No such file or directory

                    解决gcc 包之后继续安装:

                      [root@dm8 dmPython]# python setup.py  install
                      running install
                      running bdist_egg
                      running egg_info
                      writing dmPython.egg-info/PKG-INFO
                      writing top-level names to dmPython.egg-info/top_level.txt
                      writing dependency_links to dmPython.egg-info/dependency_links.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
                      building 'dmPython' extension
                      gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -DDM64 -I/dm/dmdbms/include -I/usr/include/python2.7 -c py_Dameng.c -o build/temp.linux-x86_64-2.7/py_Dameng.o -DBUILD_VERSION=2.3
                      In file included from py_Dameng.c:3:0:
                      py_Dameng.h:8:20: fatal error: Python.h: No such file or directory
                      #include <Python.h>
                      ^
                      compilation terminated.
                      error: command 'gcc' failed with exit status 1
                      [root@dm8 dmPython]#
                      复制

                      这里提示:

                        py_Dameng.h:8:20: fatal error: Python.h: No such file or directory
                        复制

                        这里是缺少python-devel 包,老方法,直接yum 解决:

                          [root@dm8 dmPython]# yum install python-devel
                          复制

                          最后终于安装成功:

                            [root@dm8 dmPython]# python setup.py install
                            ……
                            creating dist
                            creating 'dist/dmPython-2.3-py2.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.3-py2.7-linux-x86_64.egg
                            Copying dmPython-2.3-py2.7-linux-x86_64.egg to usr/lib64/python2.7/site-packages
                            Adding dmPython 2.3 to easy-install.pth file

                            Installed usr/lib64/python2.7/site-packages/dmPython-2.3-py2.7-linux-x86_64.egg
                            Processing dependencies for dmPython==2.3
                            Finished processing dependencies for dmPython==2.3
                            [root@dm8 dmPython]#
                            复制


                            ☆ 三、测试dmPython ☆

                              [root@dm8 dmPython]# python
                              Python 2.7.5 (default, Feb 20 2018, 09:19:12)
                              [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
                              Type "help", "copyright", "credits" or "license" for more information.
                              >>> import dmPython
                              /usr/lib64/python2.7/site-packages/dmPython-2.3-py2.7-linux-x86_64.egg/dmPython.py:3: UserWarning: Module dmPython was already imported from /usr/lib64/python2.7/site-packages/dmPython-2.3-py2.7-linux-x86_64.egg/dmPython.pyc, but /dm/dmdbms/drivers/python/dmPython is being added to sys.path
                              >>> conn=dmPython.connect(user='SYSDBA',password='SYSDBA',server='192.168.20.171',port=5236)
                              >>> cursor=conn.cursor()
                              >>> cursor.execute("select 'https://www.cndba.cn' from dual")
                              <__builtin__.DmdbCursor on <dmPython.Connection to SYSDBA@192.168.20.171:5236>>
                              >>> rets=cursor.fetchall()
                              >>> rets
                              [('https://www.cndba.cn',)]
                              >>> cursor.close()
                              >>> conn.close()
                              复制

                              此时通过dmPython 访问DM 数据库正常,dmPython 接口更多的详细说明可直接参考达梦数据库官方文档《DM8_dmPython 使用手册》。

                              好,以上是本次分享内容,希望能给大家带来帮助。

                              PS:本文首次发表于https://www.cndba.cn/cndba/dave/article/3827,经作者本人同意后在本公众号分享。

                              往期回顾

                              干货 |  达梦数据库大小写敏感介绍

                              总结 |  2021年达梦技术干货攻略总结,请查收!

                              开班DCA在线培训,带你快速了解达梦数据库

                              回顾丨7月份达梦培训热点回顾

                              重磅 | 这个数据库再获市场占有率第一


                               >>>>>>>>>>>> THE END <<<<<<<<<<<<

                              内容丨戴明明
                              编辑丨三横一竖
                              校对丨青城
                              文章转载自达梦E学,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                              评论