先来看一个简单的利用python调用sqlplus来输出结果的例子:
import os import sys from subprocess import Popen, PIPE sql = """ set linesize 400 col owner for a10 col object_name for a30 select owner, object_name from dba_objects where rownum<=10; """ proc = Popen(["sqlplus", "-S", "/", "as", "sysdba"], stdout=PIPE, stdin=PIPE, stderr=PIPE) proc.stdin.write(sql) (out, err) = proc.communicate() if proc.returncode != 0: print err sys.exit(proc.returncode) else: print out
用Python查询Oracle,当然最好用cx_Oracle库,但有时候受到种种限制,不能安装Python第三方库,就得利用现有资源,硬着头皮上了。
用Python调用SqlPlus查询Oracle,首先要知道SqlPlus返回结果是什么样的:
(这是空行) Number Name Address ------------ ----------- ------------------ 1001 张三 南京路 1002 李四 上海路
第1行是空行,第2行是字段名称,第3行都是横杠,有空格隔开,第4行开始是查询到的结果。
在查询结果规整的情况下,根据第3行可以很清晰的看到结构,用Python解析起来也比较方便。但是,如果一张表字段特别多,记录数也相当多,那么默认情况下调用SqlPlus查询出的结果会比较乱,这就需要在调用查询之前做一些设定,比如:
set linesize 32767 set pagesize 9999 set term off verify off feedback off tab off set 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/password@10.123.5.123: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 listResult def 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')
然后就可以用循环打印出结果了。

Arraysinpython、特にvianumpy、arecrucialinscientificComputing fortheirefficienty andversitility.1)彼らは、fornumericaloperations、data analysis、andmachinelearning.2)numpy'simplementation incensuresfasteroperationsthanpasteroperations.3)arayableminablecickick

Pyenv、Venv、およびAnacondaを使用して、さまざまなPythonバージョンを管理できます。 1)Pyenvを使用して、複数のPythonバージョンを管理します。Pyenvをインストールし、グローバルバージョンとローカルバージョンを設定します。 2)VENVを使用して仮想環境を作成して、プロジェクトの依存関係を分離します。 3)Anacondaを使用して、データサイエンスプロジェクトでPythonバージョンを管理します。 4)システムレベルのタスク用にシステムPythonを保持します。これらのツールと戦略を通じて、Pythonのさまざまなバージョンを効果的に管理して、プロジェクトのスムーズな実行を確保できます。

numpyarrayshaveveraladvantages-averstandardpythonarrays:1)thealmuchfasterduetocベースのインプレンテーション、2)アレモレメモリ効率、特にlargedatasets、および3)それらは、拡散化された、構造化された形成術科療法、

パフォーマンスに対する配列の均一性の影響は二重です。1)均一性により、コンパイラはメモリアクセスを最適化し、パフォーマンスを改善できます。 2)しかし、タイプの多様性を制限し、それが非効率につながる可能性があります。要するに、適切なデータ構造を選択することが重要です。

craftexecutablepythonscripts、次のようになります

numpyarraysarasarebetterfornumeroperations andmulti-dimensionaldata、whilethearraymoduleissuitable forbasic、1)numpyexcelsinperformance and forlargedatasentassandcomplexoperations.2)thearraymuremememory-effictientivearientfa

NumPyArraySareBetterforHeavyNumericalComputing、whilethearrayarayismoreSuitableformemory-constrainedprojectswithsimpledatatypes.1)numpyarraysofferarays andatiledance andpeperancedatasandatassandcomplexoperations.2)thearraymoduleisuleiseightweightandmemememe-ef

ctypesallowsinging andmanipulatingc-stylearraysinpython.1)usectypestointerfacewithclibrariesforperformance.2)createc-stylearraysfornumericalcomputations.3)passarraystocfunctions foreffientientoperations.how、how、becuutiousmorymanagemation、performanceo


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

SecLists
SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!

AtomエディタMac版ダウンロード
最も人気のあるオープンソースエディター

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ホットトピック









