Chinaunix首页 | 论坛 | 博客
  • 博客访问: 223204
  • 博文数量: 119
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 1261
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-23 17:50
文章分类

全部博文(119)

文章存档

2016年(8)

2015年(78)

2014年(33)

我的朋友

分类: Oracle

2015-04-16 22:39:12

Python-2.7.9.tgz  
解压再执行 make命令,编译python,成功。
下面再执行 make install。
  
[root@silent1 bin]# pwd
/usr/local/bin
[root@silent1 bin]# ls
2to3     dbhome  oraenv  python   python2.7         python2-config  smtpd.py
coraenv  idle    pydoc   python2  python2.7-config  python-config
[root@silent1 bin]# which python2
/usr/local/bin/python2
[root@silent1 bin]# python2
Python 2.7.9 (default, Apr 16 2015, 09:45:26)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>


安装或使用cx_Oracle时,需要用到Oracel的链接库,如libclntsh.so.10.1,否则会有各种各样的错误信息。
安装Oracle Instant Client就可得到这个链接库,没有必要安装Oracle Client。
root 安装的话 就需要 先配置oracle 相关变量 
cx_Oracle-5.1.3.tar.gz

cd cx_Oracle-5.1.3
python2 setup.py install


[root@silent1 cx_Oracle-5.1.3]# python2 setup.py install
running install
running build
running build_ext
building 'cx_Oracle' extension
creating build
creating build/temp.linux-x86_64-2.7-11g
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/u01/app/oracle/product/11.2.0.4/rdbms/demo -I/u01/app/oracle/product/11.2.0.4/rdbms/public -I/usr/local/include/python2.7 -c cx_Oracle.c -o build/temp.linux-x86_64-2.7-11g/cx_Oracle.o -DBUILD_VERSION=5.1.3
In file included from /u01/app/oracle/product/11.2.0.4/rdbms/public/oci.h:3045,
                 from cx_Oracle.c:10:
/u01/app/oracle/product/11.2.0.4/rdbms/public/ociap.h:10795: warning: function declaration isn’t a prototype
/u01/app/oracle/product/11.2.0.4/rdbms/public/ociap.h:10801: warning: function declaration isn’t a prototype
creating build/lib.linux-x86_64-2.7-11g
gcc -pthread -shared build/temp.linux-x86_64-2.7-11g/cx_Oracle.o -L/u01/app/oracle/product/11.2.0.4/lib -lclntsh -o build/lib.linux-x86_64-2.7-11g/cx_Oracle.so
running install_lib
copying build/lib.linux-x86_64-2.7-11g/cx_Oracle.so -> /usr/local/lib/python2.7/site-packages
running install_egg_info
Writing /usr/local/lib/python2.7/site-packages/cx_Oracle-5.1.3-py2.7.egg-info
[root@silent1 cx_Oracle-5.1.3]#

import cx_Oracle
db=cx_Oracle.connect('system','oracle','192.168.239.221:1522/prod.test.com')
print db.version
db.close()

 


import cx_Oracle
tns=cx_Oracle.makedsn('rac1',1521,'dave1')
db=cx_Oracle.connect('system','oracle',tns) --创建连接
cr=db.cursor()  --创建cursor
sql='select username , account_status from dba_users'
cr.execute(sql)  --执行sql 语句
print "\nThis is fetch-all-data !"
rs=cr.fetchall()  --一次返回所有结果集
print "print all:(%s)" %rs

print "\n print by row:"

for x in rs:
 print x

 
 
 
#### 简单测试 执行sql
>>> import cx_Oracle
>>> db=cx_Oracle.connect('system','oracle','192.168.239.221:1522/prod.test.com')
>>> print db.version
11.2.0.4.0
>>> cr=db.cursor()
>>> sql='select username , account_status from dba_users;'   #### sql 中不要包含 语句结束符 ‘;’
>>> cr.execute(sql)
Traceback (most recent call last):
  File "", line 1, in
cx_Oracle.DatabaseError: ORA-00911: invalid character

>>> sql='select username , account_status from dba_users'
>>> cr.execute(sql)
>
>>> print "\nThis is fetch-all-data !"

This is fetch-all-data !
>>> rs=cr.fetchall()
>>> print "print all:(%s)" %rs
print all:([('SYS', 'OPEN'), ('SYSTEM', 'OPEN'), ('OUTLN', 'EXPIRED & LOCKED'), ('APPQOSSYS', 'EXPIRED & LOCKED'), ('DBSNMP', 'EXPIRED & LOCKED'), ('DIP', 'EXPIRED & LOCKED'), ('ORACLE_OCM', 'EXPIRED & LOCKED')])
>>> print "\n print by row:"

 print by row:
>>> for x in rs:
...  print x
...
('SYS', 'OPEN')
('SYSTEM', 'OPEN')
('OUTLN', 'EXPIRED & LOCKED')
('APPQOSSYS', 'EXPIRED & LOCKED')
('DBSNMP', 'EXPIRED & LOCKED')
('DIP', 'EXPIRED & LOCKED')
('ORACLE_OCM', 'EXPIRED & LOCKED')
>>>

 


#### 下面是摘自网友的案例   http://www.cnblogs.com/ylqmf/archive/2012/04/16/2451841.html
三. 一个完成的示例 

 

在这个例子里,我们将用Python 对DB 进行一些操作,包括,创建一张表,并插入一些数据,在修改其中的部分数据。

 

 

[root@rac1 u01]# cat dave.py

#!/usr/bin/python

#coding=utf-8

import cx_Oracle

import sys

import urllib

import os

 

def connectDB(dbname='dave'):

        if dbname=='dave':

               

        db=cx_Oracle.connect(connstr)

        return db

 

def sqlSelect(sql,db):

        #include:select

        cr=db.cursor()

        cr.execute(sql)

        rs=cr.fetchall()

        cr.close()

        return rs

 

def sqlDML(sql,db):

        #include: insert,update,delete

        cr=db.cursor()

        cr.execute(sql)

        cr.close()

        db.commit()

 

def sqlDML2(sql,params,db):

        # execute dml with parameters

        cr=db.cursor()

        cr.execute(sql,params)

        cr.close()

        db.commit()

 

def sqlDDL(sql,db):

        #include: create

        cr=db.cursor()

        cr.execute(sql)

        cr.close()

 

 

if __name__=='__main__':

        print "This is a test python program,write by tianlesoftware!\n"

        os.environ['NLS_LANG']='SIMPLIFIED CHINESE_CHINA.UTF8'

 

        #connect to database:

        db=connectDB()

 

        #create a table:

        sql='create table dave(id number,name varchar2(20),phone number)'

        sqlDDL(sql,db)

 

        #insert data to table dave:

        sql='insert into dave values(1,\'tianlesoftware\',13888888888)'

        sqlDML(sql,db)

 

        dt=[{'id':2,'name':'dave','phone':138888888888},

            {'id':3,'name':'Oracle','phone':13888888888},

            {'id':4,'name':'anqing','phone':13888888888}]

 

        sql='insert into dave values(:id,:name,:phone)'

 

        for x in dt:

                sqlDML2(sql,x,db)

 

 

        #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)

阅读(2939) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~