在oracle中創(chuàng)建一個函數(shù),本來是想返回一個index table的,沒有成功。想到文本也可以傳輸信息,就突然來了靈感,把返回值設(shè)置文本格式。
考慮到返回數(shù)據(jù)量可能會很大,varchar2類型長度吃緊,于是將返回值類型設(shè)置為clob。
我是用scott用戶的測試表,這個是函數(shù)定義情況:
create or replace function test_query_func(dept varchar2) return clob is type test_record is record (rec_empno emp.empno%type, rec_ename emp.ename%type, rec_job emp.job%type, rec_sal emp.sal%type); type test_query_arr is table of test_record index by binary_integer; cursor cur is select empno, ename, job, sal from emp where deptno = dept; test_query test_query_arr; i integer := 0; ss varchar2(200) := ''; res clob := '['; begin for c in cur loop i := i + 1; test_query(i) := c; end loop; for q in 1..test_query.count loop ss := '(''' || test_query(q).rec_empno || ''', ''' || test_query(q).rec_ename || ''', ''' || test_query(q).rec_job || ''', ''' || test_query(q).rec_sal || ''')'; if q < test_query.count then ss := ss || ','; end if; res := res || ss; end loop; res := res || ']'; return res; end;可以在pl/sql developer測試這個函數(shù)的返回值:
begin dbms_output.put_line(test_query_func('30')); end;輸出結(jié)果:
[('7499', 'ALLEN', 'SALESMAN', '1600'),('7521', 'WARD', 'SALESMAN', '1250'),('7654', 'MARTIN', 'SALESMAN', '1250'),('7698', 'BLAKE', 'MANAGER', '2850'),('7844', 'TURNER', 'SALESMAN', '1500'),('7900', 'JAMES', 'CLERK', '950')]其實(shí)已經(jīng)定義成一個python中列表中包含元組子元素的樣式。
下面是python中的代碼:
import cx_Oracle as ora; con = ora.connect('scott/scott@oradb'); cur = con.cursor(); cur.execute('select test_query_func(30) from dual'); res = cur.fetchall()[0][0].read(); cur.close(); con.close(); data = eval(res); import pandas as pd; df = pd.DataFrame(data, columns = ['empno', 'ename', 'job', 'sal']); print(df)這樣oracle中函數(shù)返回的長字符串值就轉(zhuǎn)化為DataFrame對象了:
empno | ename | job | sal | |
---|---|---|---|---|
0 | 7499 | ALLEN | SALESMAN | 1600 |
1 | 7521 | WARD | SALESMAN | 1250 |
2 | 7654 | MARTIN | SALESMAN | 1250 |
3 | 7698 | BLAKE | MANAGER | 2850 |
4 | 7844 | TURNER | SALESMAN | 1500 |
5 | 7900 | JAMES | CLERK | 950 |
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
文章題目:用python讀取oracle函數(shù)返回值-創(chuàng)新互聯(lián)
文章來源:http://www.rwnh.cn/article18/dghsdp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、用戶體驗(yàn)、網(wǎng)站設(shè)計(jì)、云服務(wù)器、網(wǎng)站改版、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容