吴裕雄 python oracle操作数据库(4)

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)
上一篇:python迭代器以及生成器


下一篇:开源数据库连接池之DBCP