잠토의 잠망경

[python] cx_Oracle 설정 및 간단 사용 본문

공부/Python

[python] cx_Oracle 설정 및 간단 사용

잠수함토끼 2020. 2. 4. 22:10

https://github.com/yiwonjae/Project_Book_012/blob/master/sample/sample001.py

불러오는 중입니다...

사전 준비

① instant-client download

cx-oracle download


[사전 준비] instant-client download

https://www.oracle.com/database/technologies/instant-client/downloads.html

 

Oracle Instant Client Downloads

No results found Your search did not match any results. We suggest you try the following to help find what you're looking for: Check the spelling of your keyword search. Use synonyms for the keyword you typed, for example, try “application” instead of “sof

www.oracle.com

 

instant client x64

instant Client for Microsoft Windows(x64) 클릭 하고 다음으로 넘어간다.

11.2선택

나는 11g를 사용하므로 11.2를 선택하고 다운로드한다.

그리고 적당한 위치에 압축을 푼다.

나는 C Drive에 풀었다.

 


[사전 준비] cx-oracle download

cx-oracle을 다운로드 한다.


[접속] 접속 정보

CATEGORY
HOST IP
PORT port
SID sid
ID id
PW pw

 

[접속] NLS 맞추기

SELECT USERENV('LANGUAGE') FROM dual

 

밑에 환경 변수로 활용

해당 결과는 os.putenv로 활용된다.


[접속] TEST SAMPLE

일반 활용

import os
os.chdir('C:\\instantclient-basic-windows.x64-11.2.0.4.0\\instantclient_11_2')
os.putenv('NLS_LANG', 'AMERICAN_AMERICA.UTF8')

import cx_Oracle
db = cx_Oracle.connect('id','pw', 'ip:port/sid')

print('{}'.format(db.version))

sql = 'select * from dual'
cursor = db.cursor()
cursor.execute(sql)

for row in cursor:
    print(row)
    
cursor.close()
db.close()

 

pandas 활용

import os
os.chdir('C:\\instantclient-basic-windows.x64-11.2.0.4.0\\instantclient_11_2')
os.putenv('NLS_LANG', 'AMERICAN_AMERICA.UTF8')


import cx_Oracle
db = cx_Oracle.connect('id','pw', 'ip:port/sid')

print('{}'.format(db.version))


import pandas as pd

datas = pd.read_sql(sql='select * from dual', con = db)

db.close

 

 


참고

ojdbc6.jar 받는 위치

https://www.oracle.com/database/technologies/jdbc-drivers-12c-downloads.html

 

Oracle Database 12c Release 1 JDBC Driver Downloads

(875,817 bytes) - (SHA1 Checksum: 72cb63ec369807bd3a889c19ad715bf14b9adeb2) The download contains README-JDBC, README-UCP, jdbc-demo.tar, ucpdemos.jar, Javadoc-JDBC.zip, ucp_javadoc.zip, and README.txt that has more information about the contents of the ta

www.oracle.com

NLS 확인하기

SELECT * FROM NLS_DATABASE_PARAMETERS

Comments