ZhangZhihui's Blog  

 

import dmPython

def connect_dm_database():
   # 数据库连接参数
   password = 'xxxxxxxxxx'    # 数据库密码
   server = '127.0.0.1'     # 数据库服务器IP
   port = 5236                   # 数据库端口号,默认为5236

   try:
       # 连接到达梦数据库
       conn = dmPython.connect(
           password=password,
           server=server,
           port=port
       )
       print("连接成功!")

       # 创建游标对象
       cursor = conn.cursor()

       # 执行SQL查询(示例)
       cursor.execute("SELECT * FROM dual")

       # 获取查询结果
       rows = cursor.fetchall()
       for row in rows:
           print(row)

       # 关闭游标和连接
       cursor.close()
       conn.close()

   except dmPython.Error as e:
       print(f"连接失败:{e}")

# 调用函数进行连接
connect_dm_database()

 

PS E:\workspace\zzhtest> python dm.py
连接成功!
('X',)

 

posted on 2024-12-30 12:47  ZhangZhihuiAAA  阅读(36)  评论(0)    收藏  举报