import cx_Oracle

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

sql = "select * from emp"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

job = 'SALESMAN'
sql = "select * from emp where job='%s'" % job
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

job = 'SALESMAN'
sql = "select * from emp where job='%s' order by empno" % job
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

sql = "select * from dept"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

sql = "select rowid,job,ename from emp"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

sql = "select sal*(1+0.1),sal from emp"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

sql = """select empno as "%s", ename as "%s", job as "%s" from emp""" % ('员工编号','员工姓名','职务')
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

sql = """select empno "%s", ename "%s", job "%s" from emp""" % ('员工编号','员工姓名','职务')
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

sql = "select distinct job from emp"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

sql = "select * from emp where sal<>all(3000,950,800)"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

sql = "select * from emp where ename like 'S%'"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

sql = "select * from emp where job in ('%s','MANAGER','%s')" % ('PRESIDENT','ANALYST')
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

sql = "select * from emp where job not in ('%s','MANAGER','%s')" % ('PRESIDENT','ANALYST')
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

sql = "select * from emp where sal not between 2000 and 3000"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

sql = "select * from emp where sal between 2000 and 3000"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

import cx_Oracle

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

sql = "select * from locations where state_province is null"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

import cx_Oracle

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

sql = "select * from emp where sal>=2000 and sal<=3000"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

sql = "select * from emp where sal<2000 or sal>3000"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

sql = "select deptno,job from emp group by deptno,job order by deptno"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

sql = "select deptno as 员工编号,avg(sal) as 平均工资 from emp group by deptno"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

sql = "select deptno as 员工编号,avg(sal) as 平均工资 from emp group by deptno having avg(sal)>2000"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

sql = "select deptno,empno,ename from emp order by deptno,empno"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

sql = "select e.empno,e.ename,d.deptno from emp e, dept d where e.deptno=d.deptno and e.job='%s'" % ('MANAGER')
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

sql = "select e.empno,e.ename,d.dname from emp e inner join dept d on e.deptno=d.deptno"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

sql = "insert into emp(empno,ename,job) values(9527,'%s','%s')" % ('EAST','SALESMAN')
cursor.execute(sql)
conn.commit()
print("添加成功...")

sql = "select e.empno,e.ename,e.job,d.deptno,d.dname from emp e left join dept d on e.deptno=d.deptno"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

sql = "select e.empno,e.ename,e.job,d.deptno,d.dname from emp e right join dept d on e.deptno=d.deptno"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

sql = "select empno,ename,job,deptno,dname from emp natural join dept where sal>2000"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

sql = "select em2.ename,em1.ename from emp em1 left join emp em2 on em1.mgr=em2.empno order by em1.mgr"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)

sql = "select count(*) from dept cross join emp"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)