python cx_Oracle 단순 조작

3311 단어 python
1, 파일 읽 기 동작
파일 내용:
col1 col2 col3 China 1.299 0.004 4.762 USA 0.832 5.196 2.521
스 크 립 트 읽 기
#!/usr/bin/env python

import cx_Oracle
conn = cx_Oracle.connect('loge/[email protected]/prod')
c = conn.cursor()

#       ,      
list_line=[]

#       
f = open("/root/test.txt", "r")
#       ,         
line_num = 0

while True:
    #      
    line1 = f.readline()
    line_num +=1
    if line1:
        #    
if (line_num !=1): line = line1.strip('
') # line = ",".join(line.split()) # line = line.split(",") print(line) #list_line=(','.join(line.split())).split(',') #list_line.append((','.join(line.split())).split(',')) area = line[0] val1 = float(line[1]) val2 = line[2] val3 = line[3] # list_line.append((area,val1,val2,val3)) print("area:%s,val1:%.3f,val2:%s,val3:%s"%(area,val1,val2,val3)) else: break # f.close() # c.executemany('insert into t1(area,v1,v2,v3) values(:1,:2,:3,:4)',list_line) # conn.commit() # c.close() # DB cursor.close()

2, 기타 조작, 조회, 호출 과정, 함수
>>> import cx_Oracle
>>> conn=cx_Oracle.connect('loge/[email protected]/prod') 
>>> c=conn.cursor()
>>> x=c.execute("""select * from t1""")      
>>> x.fetchone()
('China', 1.299, 0.004, 4.762, None)
>>> c.close()
>>> c=conn.cursor()
>>> c.prepare("""select * from t1 where area=:id""")
>>> x=c.execute(None,{'id':'China'})
>>> for row in x:
...   print(row)
... 
('China', 1.299, 0.004, 4.762, None)
>>> c.close()
>>> conn.close()
>>> 
>>> 
>>> #    
... 
>>> conn=cx_Oracle.connect('loge/[email protected]/prod')
>>> c=conn.cursor()
>>> str1='China'
>>> str2='      '
>>> x=c.callproc('p_demo',[str1,str2])
>>> print(str2)
      
>>> print(str1)  
China
>>> str2=c.var(cx_Oracle.STRING)     
>>> x=c.callproc('p_demo',[str1,str2])
>>> print(str2)                       

>>> print(str2.getvalue)

>>> print(str2.getvalue())
China
>>> 
>>> 
>>> 
>>> #    
... 
>>> c.close()
>>> c=conn.cursor()
>>> str1='China'
>>> str2=c.callfunc('f_demo',cx_Oracle.STRING,[str1])
>>> print(str2)
China
>>> c.close()
>>> conn.colse()

첨부, 사용 과정 함수
CREATE OR REPLACE PROCEDURE P_DEMO(V1 IN VARCHAR2, V2 OUT VARCHAR2) IS
BEGIN
    V2 := V1;
END;



CREATE OR REPLACE FUNCTION F_DEMO(V1 VARCHAR2) RETURN VARCHAR2 IS
BEGIN
    RETURN V1;
END;

좋은 웹페이지 즐겨찾기