print "/nThis is Fetchall!"
rs=cr.fetchall() --一次返回所有结果集
print "print all%s)" %rs
print "/n print by row:"
for x in rs:
print x
print "/nThis is Fetone!"
cr.execute(sql)
while(1):
rs=cr.fetchone() --一次返回一行
if rs ==None:break
print rs
--使用参数查询
print "/n select with parameter:"
pr={'id':3,'tel':13888888888}
cr.execute('select * from phone where id=:id or phone=:tel',pr)
--这里我们将参数作为一个字典来处理的
rs=cr.fetchall()
print rs
cr.execute('select * from phone where id=:myid or phone=:myphone',myid=2,myphone=13888888888)
--这里我们直接写参数
rs=cr.fetchall()
print rs
cr.close()
db.close()
[root@rac1 u01]# python db.py
This is Fetchall!
print all[(1, 13865999999L), (2, 13888888888L)])
print by row:
(1, 13865999999L)
(2, 13888888888L)
This is Fetone!
(1, 13865999999L)
(2, 13888888888L)
select with parameter:
[(2, 13888888888L)]
[(2, 13888888888L)]
Python 类型和Oracle 类型的对应关系:
During the fetch stage, basic Oracle data types get mapped into their Python equivalents. cx_Oracle maintains a separate set of data types that helps in this transition. The Oracle - cx_Oracle - Python mappings are:
Oracle
cx_Oracle
Python
VARCHAR2
NVARCHAR2
LONG
cx_Oracle.STRING
str
CHAR
cx_Oracle.FIXED_CHAR
NUMBER
cx_Oracle.NUMBER
int
FLOAT
float
DATE
cx_Oracle.DATETIME
datetime.datetime
TIMESTAMP
cx_Oracle.TIMESTAMP
CLOB
cx_Oracle.CLOB
cx_Oracle.LOB
#select the result:
print "this is the first time select the data from dave"
sql='select * from dave'
rs=sqlSelect(sql,db)
for x in rs:
print x
#update data where id=1,change the name to anhui
sql='update dave set name=/'anhui/' where id=1'
sqlDML(sql,db)
#select again:
print "/n change the nanme to anhui where id equal 1,and select the result"
sql='select * from dave'
rs=sqlSelect(sql,db)
for x in rs:
print x
#delete data where id=3
sql='delete from dave where id=3'
sqlDML(sql,db)
#select again:
print "/n delete the data where id equal 3 and select the result"
sql='select * from dave'
rs=sqlSelect(sql,db)
for x in rs:
print x
db.close()
[root@rac1 u01]# python dave.py
This is a test python program,write by tianlesoftware!
this is the first time select the data from dave
(1, 'tianlesoftware', 13888888888L)
(2, 'dave', 138888888888L)
(3, 'oracle', 13888888888L)
(4, 'anqing', 13888888888L)
change the nanme to anhui where id equal 1,and select the result
(1, 'anhui', 13888888888L)
(2, 'dave', 138888888888L)
(3, 'oracle', 13888888888L)
(4, 'anqing', 13888888888L)
delete the data where id equal 3 and select the result
(1, 'anhui', 13888888888L)
(2, 'dave', 138888888888L)
(4, 'anqing', 13888888888L)
关于Python 连接Oracle 数据库,及一些基本操作,就这么多。
2个月之前,我在看Python 基础知识的时候就对自己讲,了解一下就ok,不想深入研究,可有时候,就是没得选择。 因为公司的一个脚本需要用Python来编写,所以有折腾了一下。 其实要折腾的东西还有很多。 一直没有时间去弄。
搞IT 是条不归路啊!作者: fengerapple 时间: 2013-1-25 17:23
Thank you very much for sharing!The good man!The good life of peace!作者: dragon_zlz 时间: 2013-3-4 08:54
非常感谢啊,写了好多