자바스크립트가 비활성화 되어있습니다.
자바스크립트가 활성화 되어야 콘텐츠가 깨지지 않고 보이게 됩니다.
자바스크립트를 사용할수 있도록 옵션을 변경해 주세요.
- willbsoon

본문 바로가기
Data/OracleDB

python oracleDB crud 코드

by willbsoon 2020. 9. 11.

import cx_Oracle
import traceback

# 조회
def select():
    try:
        conn=cx_Oracle.connect("test/1234@localhost:1521/xe")
        cur=conn.cursor() #커서 생성
        sql="select * from employee order by employee_id"
        cur.execute(sql)
        
        for row in cur:
            for i in range(len(row)):
                if i==3:
                    description=row[3].read() #CLOB필드 읽는 방법
                print(row[i], end=" ")
            print()
    except BaseException as e:
        print(traceback.format_exc())   
    finally:
        cur.close()
        conn.close()
#select()

# 삽입
def insert(t):
    try:
        conn=cx_Oracle.connect("test/1234@localhost:1521/xe")
        cur=conn.cursor() #커서 생성
        sql="insert into employee values(:1,:2,:3,:4,:5)"
        cur.execute(sql,t)
    except BaseException as e:
        print(traceback.format_exc())
    finally:
        cur.close()
        conn.commit()
        conn.close()
#insert((1, '개발', '홍길동', '사원', 35))

# 수정
def update(t):
    try:
        conn=cx_Oracle.connect("test/1234@localhost:1521/xe")
        cur=conn.cursor() #커서 생성
        sql="update employee set age=:1 where employee_id=:2"
        cur.execute(sql,t)
    except BaseException as e:
        print(traceback.format_exc())
    finally:
        cur.close()
        conn.commit()
        conn.close()
#update((1000,11))

# 삭제
def delete(t):
    try:
        conn=cx_Oracle.connect("test/1234@localhost:1521/xe")
        cur=conn.cursor() #커서 생성
        sql="delete from employee where employee_id=:1"
        cur.execute(sql,t)
    except BaseException as e:
        print(traceback.format_exc())
    finally:
        cur.close()
        conn.commit()
        conn.close()
#delete((10,))

댓글