import cx_Oracle
这个包需要单独下载,下载地址:https://pypi.python.org/pypi/cx_Oracle
使用yum工具安装cx_Oracle-5.2.1-1.x86_64后
在环境变量中加入:
export LD_LIBRARY_PATH=/u01/app/oracle/product/12.1.0/dbhome_1/lib
既可以开始使用cx_Oracle编写oracle相关的标本了,本次先做三个测试:
脚本1测试cx_Oracle可以正常使用
#/usr/bin/python
#coding:utf8
import cx_Oracle
conn=cx_Oracle.connect('ycr2/oracle@ycr2')
cursor=conn.cursor()
cursor.execute('select * from dual')
row=cursor.fetchone()
print row[0]
print type(row)
cursor.close()
conn.close()
脚本2创建表并插入数据
#/usr/bin/python
#coding:utf8
import cx_Oracle
conn=cx_Oracle.connect('ycr2/oracle@ycr2')
cursor=conn.cursor()
cursor.execute("insert into ycrtest1 values(2,'帅',15053282015)")
conn.commit()
cursor.close()
conn.close()
脚本3,创建查询出表中数据
#/usr/bin/python
#coding:utf8
import cx_Oracle
conn=cx_Oracle.connect('ycr2/oracle@ycr2')
cursor=conn.cursor()
cursor.execute('select * from ycrtest1')
rows=cursor.fetchall()
for row in rows:
print "%d,%s,%d" % (row[0],row[1],row[2])
print 'Number of rows returned:%d' % cursor.rowcount
cursor.execute('select * from ycrtest1')
while(1):
row=cursor.fetchone()
if row==None:
break
print '%d,%s,%s' % (row[0],row[1],row[2])
print 'Number of rows returned:%d' % cursor.rowcount
cursor.close()
conn.close()
---------------------------------------------------------------------------------------
python刚刚入门,希望以后可以写出好的脚本
Clark
2016.07.27