줄기세포

[TIBERO] Python <=> Tibero-JDBC 연동 본문

DB/Tibero

[TIBERO] Python <=> Tibero-JDBC 연동

줄기세포(Stem_Cell) 2023. 2. 20. 23:52

①. Python 과 연결

  [TIBERO 서버 내에서 Code 실행]

 

rpm 패키지, python 패키지 설치

#---- pip 설치
yum install python3-pip -y
yum install python3 -y

#---- jaydebeapi 패키지 설치
pip3 install jaydebeapi

#---- TIBERO의 DB_CHARSET은 UTF8 이다.
#---- TIBERO의 CHARSET과 맞춰주기 위해서
#---- TIBERO CLIENT의 TB_NLS_LANG 설정 필요
vi $TB_HOME/client/config/tbdsn.tbr

	TB_NLS_LANG=UTF8

#---- locale 설정
# locale -a | grep ko
export LANG=ko_KR.utf8

환경 확인

$ cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)

$ python3 --version
Python 3.6.8

$ pip3 --version
ppip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)

$ pip3 list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
JayDeBeApi (1.2.3)
JPype1 (1.3.0)
pip (9.0.3)
setuptools (39.2.0)
typing-extensions (4.1.1)

JDBC Python code

  • Tibero-jdbc는 $TB_HOME/client/lib/jar/ 경로에 위치
import os
import sys
import jaydebeapi as jp

# DB Connect
conn = jp.connect('com.tmax.tibero.jdbc.TbDriver',
                  'jdbc:tibero:thin:@localhost:8629:tibero', 
                  driver_args={"user":"tibero","password":"tmax"}, 
                  jars = "/db/tibero6/client/lib/jar/tibero6-jdbc.jar")

# Query
try:
    with conn.cursor() as curs:
        sql = """SELECT * FROM [SAMPLE_TABLE]"""
        curs.execute(sql)
        rs = curs.fetchall()
        for row in rs:
            print(row)

finally:
    conn.close()
Comments