Document Type | Technical Information
Category | Interface/Integration
Applicable Product Version | 7FS02PS
Document Number | TIITI048
Overview
This document describes the method for integrating Python with Tibero.
Test Environment
OS: RHEL 10
DB: Tibero 7.2.4
Python: 3.12
Method
1. Install Python (run as root)
A. Install Python3
yum install python3-devel
B. Change Python settings (optional)
In this environment, version 2.7 was installed and 3.12 was additionally installed.
1) Check the Python version and location set by the OS
python โV
which python
2) Change the default Python setting of the OS
update-alternatives --install /bin/python python /bin/python2.7 1
update-alternatives --install /bin/python python /bin/python3.12 2
update-alternatives --config python (select option 2)
python -V
ln -s /bin/pip3.6 /bin/pip
pip -V
3) Change settings due to yum errors
If an error occurs when running yum after changing the OS default Python version, perform the following steps:
- In the files /usr/bin/yum and /usr/libexec/urlgrabber-ext-down, change the first line #!/usr/bin/python to
#!/usr/bin/python2.7
2. Install ODBC
A. Install ODBC
yum install unixODBC (unixODBC-devel is not provided by default from RHEL 9 onwards)
B. Install pyodbc
yum install gcc-c++
pip3 install pyodbc (If you configured item 1.B, you can install using the pip command instead of pip3)
C. Configure iodbc.ini
Configure /etc/odbc.ini or create .odbc.ini in the userโs home directory (creation needed)
1) When configuring tbdsn.tbr
[ODBC Data Sources] [ODBC] [tibero7] => Must match the red name above |
2) When not configuring tbdsn.tbr
[ODBC Data Sources] [ODBC] [tibero7] => Must match the red name above Server = 192.168.131.128 => Tibero IP to connect Port = 8629 => Tibero Port to connect Database = TDB1 => Tibero DB Name to connect |
3. Install Tibero Client
A. Configure tbdsn.tbr
TDB1 =( (INSTANCE=(HOST=192.168.131.128 ) (PORT=8629) (DB_NAME=TDB1 ) ) ) |
B. Set environment variables
Add the following content to the user profile
export TB_HOME=/home/python/tibero/ export PATH=.:$TB_HOME/client/bin:$PATH export LD_LIBRARY_PATH=.:$TB_HOME/client/lib:$LD_LIBRARY_PATH
|
C. Connection test using tbsql (only possible if Tibero Client is installed)
tbsql tibero/tmax@TDB1
4. ODBC Connection Test
A. When tbdsn.tbr is configured
Test by running isql -v tibero7
5. Python + Tibero Integration
A. Confirm pyodbc import
Create a test.py file with the following content and run python test.py. There should be no errors.
| import pyodbc |
B. Integration test using Python sample code
Run the sample source below. If using Python 3 or higher, the environment variable export TBCLI_WCHAR_TYPE=ucs2 must be set.
import pyodbc
db = pyodbc.connect ('DSN=tibero;UID=tibero;PWD=tmax') db.setdecoding(pyodbc.SQL_CHAR, encoding='utf-8') db.setdecoding(pyodbc.SQL_WCHAR, encoding='utf-8') db.setdecoding(pyodbc.SQL_WMETADATA, encoding='utf-32le') db.setencoding(encoding='utf-8') cursor = db.cursor()
cursor.execute('select * from python_test;') data = cursor.fetchall()
for x in data: print (x[0]) -- The following line after the Python for loop should be indented
cursor.close() db.close()
The red code lines are required if encoding-related errors occur |