python 讀寫Oracle10g資料簡介

來源:互聯網
上載者:User

標籤:style   blog   http   ar   color   os   sp   for   strong   

1、測試環境:

Centos6 X86_64
python 2.6
Oracle 10g

2、安裝cx_Oracle 和 Oracle InstantClient:

http://www.rpmfind.net/linux/rpm2html/search.php?query=cx_oracle
http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html

3、編輯目前使用者的 .bash_profile, 在檔案末尾增加下行:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/oracle/10.2.0.3/client64/lib

命令列執行 source .bash_profile

4、現在就可以用 python 指令碼非常輕鬆的讀寫Oracle資料庫

資料查詢示範指令碼 select_ora.py

# This script prints the data in Oracle Sample table scott.emp .# Run with a parameter ‘PageSize‘ in integer form, the output pauses at the end of every page.# Run without a parameter or the parameter is not in integer form, the output doesn‘t pasue.# Like: 
# $ python select_ora.py 30
# $ python select_ora.py
#import sysimport cx_Oracletry: intPageSize = int(sys.argv[1])except: intPageSize = -1 #print "Please input an integer." #quit() conn = cx_Oracle.connect(‘scott/[email protected]/c6115‘)cursor = conn.cursor ()cursor.execute ("select * from emp")print "EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO"print "========================================================"while (1): row = cursor.fetchone() if row == None: break print "%d, %s, %s, %s, %s, %s, %s, %s" % (row[0], row[1], row[2], row[3], row[4],row[5],row[6],row[7]) if intPageSize <> -1 and cursor.rowcount % intPageSize == 0 : strPress = raw_input( "Row: %d" % cursor.rowcount + ". Press Enter to continue, q to quit..." ) if strPress == ‘q‘: break print "Number of rows returned: %d" % cursor.rowcountcursor.close ()conn.close ()

 

資料插入示範指令碼 insert_ora.py

import cx_OraclestartNum = raw_input("Start Num:")endNum = raw_input("End Num:") conn = cx_Oracle.connect(‘scott/[email protected]/c6115‘)cursor = conn.cursor()i = int(startNum)while (1):    i = i+1    if i > int(endNum):        break     theNum=str(i)    cursor.execute("insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) values("+theNum+",‘FORD"+theNum+"‘,‘CLERK‘,7782,‘09-JUN-81‘,2500,0,30)")    conn.commit()    print "Line "+ theNum +" inserted."cursor.close()conn.close()

 

參考: http://blog.csdn.net/kongxx/article/details/7107661

python 讀寫Oracle10g資料簡介

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.