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