This commit is contained in:
2021-11-14 15:52:46 +08:00
parent 915c231124
commit 1e344dc204
112 changed files with 1039 additions and 1039 deletions

View File

@@ -6,30 +6,30 @@ tags: ["python", "cx_oracle"]
categories: ["python"]
---
# 导入 cx_Oracle 模块
## 导入 cx_Oracle 模块
```
import cx_Oracle # 导入模块
```
# 连接数据库
## 连接数据库
```
db = cx_Oracle.connect('user', 'password', 'host:port/SID') #建立连接,3个参数分开写
print db.version
#输出 10.2.0.1.0 测试成功
##输出 10.2.0.1.0 测试成功
```
# 自动提交
## 自动提交
```
db.autocommit=True #开启自动提交
db.autocommit=False #关闭自动提交
```
# 建立 cursor 光标
## 建立 cursor 光标
```
cursor = db.cursor() #建立一个cursor
```
# 执行sql
## 执行sql
```
cursor.execute(select * from tabs) # 执行一条sql
sql = "insert into person(name, age, telephone) values(%s, %s, %s)"
@@ -37,7 +37,7 @@ 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),...]
@@ -47,17 +47,17 @@ for x in row:
print cursor.rowcount() #获取输出记录数量
```
# 提交
## 提交
```
db.commit()
```
# 回滚
## 回滚
```
db.rollback()
```
# 关闭连接
## 关闭连接
```
cursor.close()
db.close()