Python/데이터 분석

(데이터 분석) 파이썬 - 오라클 연동

choisanghyun 2021. 12. 31. 17:40
728x90
import cx_Oracle as cx

# conn = cx.connect("ai","0000","127.0.0.1:1521/XE")
# if bool(conn):
#     print("연결성공")
# else:
#     print("연결실패")
#
# cur = conn.cursor() #커서로 데이터 접근가능
# cur.execute("select * from emp") #emp 데이터 가져와라 (SQL문)
# for c in cur:
#     print(c) #() 튜플 : 수정,삭제 불가
#
# cur.close() #커넥션 한번 열었으면 다시 닫아주기 연결 많아지면 오라클 서버 과부화
# conn.close()

#------------------------------------------------------------

#---------------------------------------------------------------
def my_select(ename=None): #줘도그만 안줘도그만 None
    conn = cx.connect("ai", "0000", "127.0.0.1:1521/XE")
    if bool(conn):
        print("연결성공")
    else:
        print("연결실패")

    cur = conn.cursor()  # 커서로 데이터 접근가능

    if bool(ename):
        sql = f"select * from emp where ename='{ename}'"  # emp 데이터 가져와라 (SQL문)
    else:
        sql = "select * from emp"
    cur.execute(sql)

    for c in cur:
        print(c)  # () 튜플 : 수정,삭제 불가

    cur.close()  # 커넥션 한번 열었으면 다시 닫아주기 연결 많아지면 오라클 서버 과부화
    conn.close()

#------------------------------호출---------------------
# my_select('KING')
# my_select()