This commit is contained in:
2021-11-14 14:32:08 +08:00
parent f75ad8bedd
commit b0f6120151
152 changed files with 22219 additions and 8 deletions

View File

@@ -0,0 +1,65 @@
---
title: "Python 的 cx_Oracle 模块"
date: 2019-10-30T17:55:41+08:00
lastmod: 2019-10-30T17:55:41+08:00
tags: ["python", "cx_oracle"]
categories: ["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()
```