www.colben.cn/content/post/python-cx_Oracle.md
2021-11-14 15:52:46 +08:00

1.2 KiB
Raw Permalink Blame History

title, date, lastmod, tags, categories
title date lastmod tags categories
Python 的 cx_Oracle 模块 2019-10-30T17:55:41+08:00 2019-10-30T17:55:41+08:00
python
cx_oracle
python

导入 cx_Oracle 模块

import cx_Oracle    # 导入模块

连接数据库

db = cx_Oracle.connect('user', 'password', 'host:port/SID') #建立连接,3个参数分开写
print db.version
##输出 10.2.0.1.0 测试成功

自动提交

db.autocommit=True #开启自动提交
db.autocommit=False #关闭自动提交

建立 cursor 光标

cursor = db.cursor()    #建立一个cursor

执行sql

cursor.execute(select * from tabs)    # 执行一条sql
sql = "insert into person(name, age, telephone) values(%s, %s, %s)"
tmp = (('ninini', 89, '888999'), ('koko', 900, '999999'))
conn.executemany(sql, tmp) #执行多条sql

获取执行结果

row=cursor.fetchone() #取一行结果,元组(a,b,c,d)
row=cursor.fetchall() #获取所有结果,列表[(a,b,c,d),(e,f,g,h),...]
for x in row:
    For y in x:
        Print y
print cursor.rowcount() #获取输出记录数量

提交

db.commit()

回滚

db.rollback()

关闭连接

cursor.close()
db.close()