<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
更复杂的例子
其他例子
http://blog.csdn.net/liukeforever/article/details/6220429
注意 :使用fetchall 的时候要慎重print 因为数据量太大可能导致print失效 卡python;同样pycharm中 output不能too much
pycharm对cx_Oracle模块不识别,可能要卸载,再用easy_install安装一遍
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安装一遍
相关文章推荐
- <PY>iPad/iPhone上Python配置
- <转载>PL/SQL Developer如何连接64位的Oracle图解
- <寒江独钓>Windows内核安全编程__键盘过滤之内核级Hook(一)
- 原创:oracle PL/SQL编程基础 上<十一>
- struts2配置web的action中的<url-pattern>
- <h1>读取配置文件失败</h1>这通常意味着文件中有语法错误,请检查下面显示出的错误。<br /><br /><iframe src="show_config_errors.php" />
- <寒江独钓>Windows内核安全编程__键盘过滤之内核级Hook(二)
- 只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态。还请确保在应用程序配置的 <configuration>
- 使用<appSettings>元素定义配置参数
- <<svn服务配置和维护常用命令>>
- <备份>0926网络配置与while for until
- 原创:oracle:sql介绍及SQL基本查询 <四>
- <Ibatis in action>中使用动态SQL的一个小细节提示(与CDATA)
- <<<I、O概述及字符流基本操作>>>
- <配置> Ubuntu gcc 安装 使用方法
- web.xml配置中的<mime-mapping>
- <寒江独钓>Windows内核安全编程__一个简单的Windows串口过滤驱动程序的开发
- <input>回车后调用JS进行指定操作
- applicationContext-security.xml配置中的<http></http>标签的意义
- <Action>和<result>配置中的各项默认值