您的位置:首页 > 数据库 > Oracle

<PY><Oracle>Windows下cx_Oracle配置 操作SQL

2014-08-06 16:02 441 查看
参考帖子

http://blog.csdn.net/magicboylinw/article/details/7025885

/article/1416047.html

http://gypsyer.blog.51cto.com/734537/163344

http://bbs.chinaunix.net/thread-1753497-1-1.html

1.下载对应版本的Oracle Instant Client(指的是python版本,不是系统版本)

下载对应版本的cx_Oracle包(指的是python版本,不是系统版本)

2.先配好python的环境变量 系统变量 PATH里添加 ;C:\Python27;C:\Python27\Scripts

双击安装cx_Oracle-5.1.2-10g.win32-py2.7.msi

#zip gz形式的话(使用cmd或者powershell)定位到cx_Oracle 解压好后的文件夹 使用命令python setup.py install

3.将Oracle Instant Client 解压到C盘根目录(其他也可以)

拷贝oraociei11.dll、oraocci11.dll、oci.dll三个文件到C:\Python27\Lib\site-packages #为了防止ImportError: DLL load failed:

新建三个系统变量

ORACLE_HOME = C:\instantclient_11_2

TNS_ADMIN = C:\instantclient_11_2

NLS_LANG = SIMPLIFIED CHINESE_CHINA.ZHS16GBK

3(2).上面的方式容易出错

可以直接使用(管理员右键)Instant Client Setup(11.2.0.3x86).exe免去一切配置 地址 http://pan.baidu.com/s/1hqrHPje

编辑配置文件http://evil850209.iteye.com/blog/1394932

4.新建注册表 \HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE 下(如果没有该目录,创建一个) 键 NLS_LANG 字符串 值 NA #防止ORA-12705错误

配置完成

下面做个demo

import cx_Oracle    
conn = cx_Oracle.connect('fkong/fkong@172.17.23.129/orcl')    
cursor = conn.cursor ()  
cursor.execute ("select * from dual")  
row = cursor.fetchone ()  
print row[0]    
cursor.close ()  
conn.close ()


更复杂的例子

import cx_Oracle
import datetime

def phone_num_make(tablelist,selectstep,maxlinesinafile):
    cnn=cx_Oracle.connect('usr','passwd','address/databasename')
    cursor=cnn.cursor()
    phonelist=[]
    count=[]
    searched=0
    length=0
    writecount=0
    writename=0
    for table in tablelist:
        try:
            starttime=datetime.datetime.now()
            tmp=starttime
            cursor.execute('select * from %s'%table)
            while 1:
                data=cursor.fetchone()
                searched+=1
                if searched%selectstep==0:
                    try:
                        w=open('searched.txt','a')
                        tmp1=datetime.datetime.now()
                        w.write('%d searched '%searched+str(tmp1-tmp)+' '+str(tmp1-starttime)+'\n')
                        w.close()
                        tmp=tmp1
                    except:
                        pass

                if data:
                    try:
                        count[phonelist.index(data[0])]+=1
                    except:
                        phonelist.append(data[0])
                        count.append(1)
                        length+=1
                        #if length%selectstep==0:
                            #print length ,"added"

                else:
                    print "%s all_searched  time= "%table,datetime.datetime.now()-starttime
                    break
        except:
            print "failed:something wrong with searching %s"%table

    while writecount<length:
        f=open('phone_num%d.txt'%writename,'w')
        for i in range(writecount,length):
            f.write(str(phonelist[i])+' ')
            f.write(str(count[i])+'\n')
            writecount+=1
            if writecount%maxlinesinafile==0:
                f.close()
                print "file %d written!"%writename
                writename+=1
                break
    try:
        f.close()
    except:
        pass
    try :
        out=open('output.txt','w')
        out.write('%d s'%writecount)
        out.write(" phones")
        out.write('\n%d files written'%(writename+1))
        out.write("\n time used:%s"%(datetime.datetime.now()-starttime))
    except:
        print "output file cannot be written"
    print 'phones=',writecount
    print 'all_table_written!',datetime.datetime.now()-starttime

if __name__=='__main__':
    tablelist=['tjrecords_140504']
    #for i in range(140504,140516):
    #    tablelist.append('tjrecords_%d'%i)
    #print tablelist
    selectstep=20000
    maxlinesinafile=10000000
    phone_num_make(tablelist,selectstep,maxlinesinafile)
    print 'project done!'


其他例子

http://blog.csdn.net/liukeforever/article/details/6220429

注意 :使用fetchall 的时候要慎重print 因为数据量太大可能导致print失效 卡python;同样pycharm中 output不能too much

pycharm对cx_Oracle模块不识别,可能要卸载,再用easy_install安装一遍





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: