dmPython的使用

1、环境

(1)已经安装配置好dmPython

(2)安装pycharm(也可以不用这个)

2、连接数据库

import dmPython
try:
    conn = dmPython.connect(user='SYSDBA', password='SYSDBA', server='localhost',  port=5236)
    cursor  = conn.cursor()
    print('python: conn success!')
    conn.close()
except (dmPython.Error, Exception) as err:
    print(err)

 3、增删改查的操作

#!/usr/bin/python
#coding:utf-8
import dmPython

try:
    conn = dmPython.connect(user='SYSDBA',password='SYSDBA',server='localhost',port=5236)
    cursor =conn.cursor()
    print('python:conn success!')
    try:
        print('python: drop success!')
    except(dmPython.Error, Exception) as err:
        print(err)
    try:
        #create table
        cursor.execute("create table test(id int,name varchar(20))")

        #insert date
        cursor.execute("insert into test(id,name) values(1,'chinese')")
        cursor.execute("insert into test(id,name) values(2,'math')")
        #update data
        cursor.execute("update test set name=\'English\' where name = \'chinese\'")
        cursor.execute("select * from test")
        res = cursor.fetchall()
        print(type(res))
        for i in res:
            print(type(i))
            print(i)
            for j in i:
                print(j)

        print('python: select success!')
        cursor.execute("commit")

    except(dmPython.Error,Exception) as err:
        print(err)


    conn.close()

 4、绑定变量实例

#!/usr/bin/python
#coding:utf-8
import dmPython
try:
    conn = dmPython.connect(user='SYSDBA', password='SYSDBA', server='localhost',  port=5236)
    cursor  = conn.cursor()
    try:
        #清空表,初始化测试环境
        cursor.execute ('delete from test')
    except (dmPython.Error, Exception) as err:
        null

    try:
        #插入数据
        values = ('物理')
        cursor.execute ("insert into test(name) values(?)", values)
        print('python: insert success!')

        #查询数据
        cursor.execute ("select name from test")
        res = cursor.fetchall()
        for tmp in res:
            for c1 in tmp:
                print c1

        print('python: select success!')
    except (dmPython.Error, Exception) as err:
        print(err)


    conn.close()
except (dmPython.Error, Exception) as err:
    print(err)

 更多资讯请上达梦技术社区了解:https://eco.dameng.com

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值