使用dmpython连接达梦集群数据库,首次连接可以查询并成功返回,再次连接时,再也连接不上了。
写了一个连接类
import dmPython
import traceback
class DaMengDBClient:
def __init__(self, host, user, pwd):
self.host = host
self.user = user
self.pwd = pwd
self.collectionstrurl = "%s/%s@%s" % (self.user, self.pwd, self.host)
def __enter__(self):
try:
self.cnxn = dmPython.connect(self.collectionstrurl)
self.cursor = self.cnxn.cursor()
return self
except Exception as e:
print(e)
print('数据库连接失败!')
print(traceback.format_exc())
def __exit__(self, exc_type, exc_val, exc_tb):
self.cursor.close()
self.cnxn.close()
if __name__ == '__main__':
host = 'DamengDSC'
user = 'test'
pwd = '123456'
with DaMengDBClient(host=host, user=user, pwd=pwd) as oc:
sql = 'select * from "test"'
# 执行SQL语句
values = oc.cursor.execute(sql).fetchall()
print(values)
第一次成功后,第二次
报错:[-70028]:创建 SOCKET 连接失败
不知道为什么,直接用字符串不行,用用户名/密码@服务名拼接可以
cnxn = dmPython.connect('SYSDBA/SYSDBA@DamengDSC') # 不行
这样可以了
cnxn = dmPython.connect("%s/%s@%s" % (self.user, self.pwd, self.host))
请发一个完整得样例文件