博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
吴裕雄 python oracle操作数据库(4)
阅读量:4926 次
发布时间:2019-06-11

本文共 2358 字,大约阅读时间需要 7 分钟。

import cx_Oracle

conn = cx_Oracle.connect("scott/admin@localhost:1521/orcl")

cursor = conn.cursor()

sql = "insert into dept (deptno,dname,loc) values ('%d','%s','%s')" % (88,'design','beijing')

cursor.execute(sql)
conn.commit()
print('添加成功...')

添加成功...

import cx_Oracle

conn = cx_Oracle.connect("hr/admin@localhost:1521/orcl")

cursor = conn.cursor()

sql = "insert into jobs values ('%s','%s','%d','%d')" % ('PRO','程序员',5000,10000)

cursor.execute(sql)
conn.commit()
print('添加成功...')

添加成功...

sql = """

create table jobs_temp(
job_id varchar2(10) primary key,
job_title varchar2(35) not null,
min_salary number(6),
max_salary number(6))
"""
cursor.execute(sql)
conn.commit()
print('表创建成功...')

表创建成功...

sql = """

insert into jobs_temp
select * from jobs
where jobs.max_salary > 10000
"""
cursor.execute(sql)
conn.commit()
print('添加成功...')

添加成功...

import cx_Oracle

conn = cx_Oracle.connect("scott/admin@localhost:1521/orcl")

cursor = conn.cursor()

sql = "update emp set sal=sal*1.2 where job='SALESMAN'"

cursor.execute(sql)
conn.commit()
print('更新成功...')

更新成功...

sql = "update emp set sal=(select avg(sal) from emp where job='SALESMAN') where sal<2000"

cursor.execute(sql)
conn.commit()
print('更新成功...')

更新成功...

import cx_Oracle

conn = cx_Oracle.connect("hr/admin@localhost:1521/orcl")

cursor = conn.cursor()

sql = "delete from jobs where job_id='PRO'"

cursor.execute(sql)
conn.commit()
print('delete成功...')

delete成功...

sql = "truncate table jobs_temp"

cursor.execute(sql)
conn.commit()
print('truncate成功...')

truncate成功...

sql = """insert into jobs_temp values ('DESIGN','设计人员',3000,5000)"""

cursor.execute(sql)
conn.commit()
print('添加成功...')

添加成功...

sql = "truncate table jobs_temp"

cursor.execute(sql)
conn.commit()
print('truncate成功...')
sql = """insert into jobs_temp values ('OFFICE','办公文员',3000,5000)"""
cursor.execute(sql)
print('添加成功...下一步设置了保存点...')
sql = """savepoint sp"""
cursor.execute(sql)
sql = """insert into jobs_temp values ('FINANCE','财务人员',3000,5000)"""
cursor.execute(sql)
print('添加成功...下一步回滚到保存点...')

sql = """rollback to savepoint sp"""

cursor.execute(sql)
conn.commit()
print('提交成功...')
sql = """select * from jobs_temp"""
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

truncate成功...添加成功...下一步设置了保存点...添加成功...下一步回滚到保存点...
提交成功...('OFFICE', '办公文员', 3000, 5000)

转载于:https://www.cnblogs.com/tszr/p/10125079.html

你可能感兴趣的文章
重视知识的本质
查看>>
为什么linux驱动中变量或者函数都用static修饰?(知乎问题)
查看>>
课后作业2:个人项目
查看>>
初猎《梦断代码》
查看>>
短信SMS接口
查看>>
Angular滚动到底部自动加载
查看>>
do-while语句
查看>>
Multiple ComboBox的赋值取值
查看>>
永不消逝的电波
查看>>
ZC_float_测试
查看>>
Linux CentOS7下安装python3
查看>>
《Redis设计与实现》读书笔记
查看>>
解决Json的DateTime格式问题
查看>>
Access-Control-Allow-Origin php跨域报错
查看>>
普通行列转换(version 2.0)(转载)
查看>>
Team Foundation Server 2010 – Basic Installation(转载)
查看>>
T-SQL查询的逻辑处理过程
查看>>
nginx.conf配置文件详解
查看>>
python lambda匿名函数
查看>>
SQL Server数据库入门学习总结
查看>>