日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python达梦数据库_Python 封装 DM 达梦 数据库操作(使用类封装基本的增删改查)...

發布時間:2023/12/10 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python达梦数据库_Python 封装 DM 达梦 数据库操作(使用类封装基本的增删改查)... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前面的幾篇博客我們看了SQLite、Mysql、Oracle數據庫的封裝,本篇看下達夢數據庫的封裝.

https://www.cndba.cn/Dave/article/3827

1 測試環境

操作系統本:

[root@dm8 dmPython]# cat /etc/redhat-release

Red Hat Enterprise Linux Server release 7.5 (Maipo)

[root@dm8 dmPython]#

達夢數據庫版本:

[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 是測試版,所以版本和官方下載的不一樣。

2 安裝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

命令雖簡單,但過程插曲比較多。https://www.cndba.cn/Dave/article/3827https://www.cndba.cn/Dave/article/3827

2.1 插曲一:

[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.2 插曲二:

切換成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

raise DistutilsSetupError("cannot locate an Dameng software " /

distutils.errors.DistutilsSetupError: cannot locate an Dameng software installation

[root@dm8 dmPython]#

因為安裝依賴DM的環境,所以這里想了一個解決方法,在root用戶下配置dm的環境變量。

https://www.cndba.cn/Dave/article/3827

[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]#

2.3 插曲三:

解決上面的問題后繼續安裝:

https://www.cndba.cn/Dave/article/3827

[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 安裝:

關于yum 的配置,參考我的博客:

2.4 插曲四:

解決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

^

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]#

2.5 測試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 to sysdba>>

>>> rets=cursor.fetchall()

>>> rets

[('https://www.cndba.cn',)]

>>> cursor.close()

>>> conn.close()

通過dmPython 訪問DM 數據庫沒有問題,dmPython 接口更多的詳細說明可以直接參考官方文檔《DM8_dmPython 使用手冊》,我們這里不在進行測試。https://www.cndba.cn/Dave/article/3827

3 封裝類

3.1 Python 代碼

# -*- coding: utf-8 -*-

# @Time : 2019-11-27 22:52

# @Author : cndba

# @FileName: MyDM.py

# @Software: PyCharm

# @Blog :https://www.cndba.cn/dave

import dmPython

class MyDM:

SHOW_SQL = True

def __init__(self, host='127.0.0.1', port=5236, user='SYSDBA', password='SYSDBA'):

self.host = host

self.port = port

self.user = user

self.password = password

def get_con(self):

try:

conn = dmPython.connect(user=self.user, password=self.password, server=self.host, port=self.port, autoCommit=True)

return conn

except dmPython.Error, e:

print "dmPython Error:%s" % e

def select_all(self, sql):

try:

con = self.get_con()

# print con

cur = con.cursor()

cur.execute(sql)

fc = cur.fetchall()

return fc

except dmPython.Error, e:

print "dmPython Error:%s" % e

finally:

cur.close()

con.close()

def select_by_where(self, sql, data):

try:

con = self.get_con()

# print con

d = (data,)

cur = con.cursor()

cur.execute(sql, d)

fc = cur.fetchall()

# if len(fc) > 0:

# for e in range(len(fc)):

# print(fc[e])

return fc

except dmPython.Error, e:

print "dmPython Error:%s" % e

finally:

cur.close()

con.close()

def dml_by_where(self, sql, params):

try:

con = self.get_con()

cur = con.cursor()

for d in params:

if self.SHOW_SQL:

print('執行sql:[{}],參數:[{}]'.format(sql, d))

cur.execute(sql, d)

con.commit()

except dmPython.Error, e:

con.rollback()

print "dmPython Error:%s" % e

finally:

cur.close()

con.close()

# 不帶參數的更新方法

def dml_nowhere(self, sql):

try:

con = self.get_con()

cur = con.cursor()

count = cur.execute(sql)

con.commit()

return count

except dmPython.Error, e:

con.rollback()

print "dmPython Error:%s" % e

finally:

cur.close()

con.close()

# 開始測試函數

def select_all():

sql = "select * from dave"

fc = db.select_all(sql)

for row in fc:

print row

def select_by_where():

sql = "select * from dave where USER_ID=:1"

data = '0551'

fc = db.select_by_where(sql, data)

for row in fc:

print row

def ins_by_param():

sql = "insert into dave(USERNAME,USER_ID) values(:1,:2)"

data = [('https://www.cndba.cn', '0551'), ('https://www.cndba.cn/dave', '0556')]

db.dml_by_where(sql, data)

def del_by_where():

sql = "delete from dave where USERNAME = :1 or USER_ID=:2"

data = [('huaining', '0556')]

db.dml_by_where(sql, data)

def update_by_where():

sql = "update dave set USER_ID=:1 where USER_ID=:2"

data = [('0556', '0551')]

db.dml_by_where(sql, data)

def del_nowhere():

sql = "delete from dave"

print db.dml_nowhere(sql)

if __name__ == "__main__":

db = MyDM( '192.168.20.171', port=5236, user='SYSDBA', password='SYSDBA')

# ins_by_param()

# select_by_where()

# del_by_where()

# select_all()

# update_by_where()

del_nowhere()

select_all()

這里根據對應的測試注釋或啟用相關的函數。

3.2 執行過程如下

注意我這里測試之前先創建好了測試表dave:

SQL> create table dave(username varchar(50),user_id varchar(10));

[root@dm8 Code]# python MyDM.py

執行sql:[insert into dave(USERNAME,USER_ID) values(:1,:2)],參數:[('https://www.cndba.cn', '0551')]

執行sql:[insert into dave(USERNAME,USER_ID) values(:1,:2)],參數:[('https://www.cndba.cn/dave', '0556')]

('https://www.cndba.cn', '0551')

('https://www.cndba.cn/dave', '0556')

[root@dm8 Code]# python MyDM.py

('https://www.cndba.cn', '0551')

[root@dm8 Code]# python MyDM.py

執行sql:[delete from dave where USERNAME = :1 or USER_ID=:2],參數:[('huaining', '0556')]

('https://www.cndba.cn', '0551')

[root@dm8 Code]# python MyDM.py

執行sql:[update dave set USER_ID=:1 where USER_ID=:2],參數:[(361, 366)]

('https://www.cndba.cn', '0551')

[root@dm8 Code]# python MyDM.py

執行sql:[update dave set USER_ID=:1 where USER_ID=:2],參數:[('0551', '0556')]

('https://www.cndba.cn', '0551')

[root@dm8 Code]# python MyDM.py

執行sql:[update dave set USER_ID=:1 where USER_ID=:2],參數:[('0551', '0556')]

('https://www.cndba.cn', '0551')

[root@dm8 Code]# python MyDM.py

執行sql:[update dave set USER_ID=:1 where USER_ID=:2],參數:[('0551', '0556')]

('https://www.cndba.cn', '0551')

[root@dm8 Code]# python MyDM.py

執行sql:[update dave set USER_ID=:1 where USER_ID=:2],參數:[('0556', '0551')]

('https://www.cndba.cn', '0556')

[root@dm8 Code]# python MyDM.py

None

[root@dm8 Code]#

基本測試完成,更多操作可以參考dmPython 文檔。

版權聲明:本文為博主原創文章,未經博主允許不得轉載。

總結

以上是生活随笔為你收集整理的python达梦数据库_Python 封装 DM 达梦 数据库操作(使用类封装基本的增删改查)...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。