Python調用SqlPlus查詢Oracle

來源:互聯網
上載者:User

標籤:

用Python查詢Oracle,當然最好用cx_Oracle庫,但有時候受到種種限制,不能安裝Python第三方庫,就得利用現有資源,硬著頭皮上了。

用Python調用SqlPlus查詢Oracle,首先要知道SqlPlus返回結果是什麼樣的:

(這是空行)Number       Name        Address------------ ----------- ------------------1001         張三         南京路1002         李四         上海路

第1行是空行,第2行是欄位名稱,第3行都是橫杠,有空格隔開,第4行開始是查詢到的結果。

在查詢結果規整的情況下,根據第3行可以很清晰的看到結構,用Python解析起來也比較方便。但是,如果一張表欄位特別多,記錄數也相當多,那麼預設情況下調用SqlPlus查詢出的結果會比較亂,這就需要在調用查詢之前做一些設定,比如:

set linesize 32767set pagesize 9999set term off verify off feedback off tab offset numwidth 40

這樣的調用查詢結果就比較規整了。接下來就是用強大的Python來解析查詢結果。

這裡封裝了一個函數,可以根據傳入的SQL語句查詢並解析結果,將每行結果存到列表中,列表中的每個元素是一個欄位名稱與值的映射。

#!/usr/bin/python#coding=UTF-8‘‘‘@author: 雙子座@開源中國@summary: 通過SqlPlus查詢Oracles資料庫‘‘‘import os;os.environ[‘NLS_LANG‘] = ‘AMERICAN_AMERICA.AL32UTF8‘gStrConnection = ‘username/[email protected]:1521/ora11g‘#解析SqlPlus的查詢結果,返回列表def parseQueryResult(listQueryResult):    listResult = []    #如果少於4行,說明查詢結果為空白    if len(listQueryResult) < 4:        return listResult    #第0行是空行,第1行可以擷取欄位名稱,第2行可擷取SQLPlus原始結果中每列寬度,第3行開始是真正輸出    # 1 解析第2行,取得每列寬度,放在列表中    listStrTmp = listQueryResult[2].split(‘ ‘)    listIntWidth = []    for oneStr in listStrTmp:        listIntWidth.append(len(oneStr))    # 2 解析第1行,取得欄位名稱放在列表中    listStrFieldName = []    iLastIndex = 0    lineFieldNames = listQueryResult[1]    for iWidth in listIntWidth:        #截取[iLastIndex, iLastIndex+iWidth)之間的字串        strFieldName = lineFieldNames[iLastIndex:iLastIndex + iWidth]        strFieldName = strFieldName.strip() #去除兩端空白符        listStrFieldName.append(strFieldName)        iLastIndex = iLastIndex + iWidth + 1    # 3 第3行開始,解析結果,並建立映射,儲存到列表中    for i in range(3, len(listQueryResult)):        oneLiseResult = unicode(listQueryResult[i], ‘UTF-8‘)        fieldMap = {}        iLastIndex = 0        for j in range(len(listIntWidth)):            strFieldValue = oneLiseResult[iLastIndex:iLastIndex + listIntWidth[j]]            strFieldValue = strFieldValue.strip()            fieldMap[listStrFieldName[j]] = strFieldValue            iLastIndex = iLastIndex + listIntWidth[j] + 1        listResult.append(fieldMap)    return listResultdef QueryBySqlPlus(sqlCommand):    global gStrConnection    #構造查詢命令    strCommand = ‘sqlplus -S %s <<!\n‘ % gStrConnection    strCommand = strCommand + ‘set linesize 32767\n‘    strCommand = strCommand + ‘set pagesize 9999\n‘    strCommand = strCommand + ‘set term off verify off feedback off tab off \n‘    strCommand = strCommand + ‘set numwidth 40\n‘    strCommand = strCommand + sqlCommand + ‘\n‘    #調用系統命令收集結果    result = os.popen(strCommand)    list = []    for line in result:        list.append(line)    return parseQueryResult(list)

其中os.environ[‘NLS_LANG‘]的值來自

select userenv[‘language‘] from dual;

在調用的時候,只要類似:

listResult = QueryBySqlPlus(‘select * from studentinfo‘)

然後就可以用迴圈列印出結果了。

以上方法假定查詢操作沒有資料庫錯誤,能力有限。

Python調用SqlPlus查詢Oracle

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.