Linux下Python3通过PYODBC连接到GBase 8s数据库

Linux下Python3通过PYODBC连接到GBase 8s数据库
操作系统:CentOS 7.8 64-bit
软件需求:
GBase 8s CSDK 2.0.1A2_2
Python 3.6.8 及Python-devel 3.6.8
unixODBC 2.3.1
pyodbc 4.0.30

1,系统环境确认

1.1 python、python-devel及unixODBC版本

Linux下root用户通过rpm确认python3、python3-devel,unixODBC、unixODBC-devel均已经安装,如下:

[root@a01 ~]# rpm -qa python3 python3-devel unixODBC unixODBC-devel
python3-devel-3.6.8-13.el7.x86_64
unixODBC-2.3.1-14.el7.x86_64
unixODBC-devel-2.3.1-14.el7.x86_64
python3-3.6.8-13.el7.x86_64

1.2 GBase 8s CSDK已经安装

Linux下gbasedbt用户通过esql -V命令确认GBase 8s CSDK已经安装,如下:

[gbasedbt@a01 ~]$ esql -V
GBase CSDK Version 4.10, GBASE-ESQL Version 4.10.FC4G1_2.0.1A2_2
Software Serial Number AAA#B000000

2,ODBC配置

2.1 ODBC配置文件

修改/etc/odbc.ini配置文件,增加GBase 8s数据库连接信息,特别注意需要的环境变量都加上。

;---------------------------------------------------------------------------
; GBase ODBC Sample File
;
; File:         odbc.ini
;
;---------------------------------------------------------------------------
[ODBC Data Sources]
utf8=GBase ODBC DRIVER
;
; Define ODBC Database Driver's Below - Driver Configuration Section
;
[utf8]
Driver=/opt/gbase/lib/cli/iclis09b.so
Description=GBase ODBC DRIVER
Database=utf8
LogonID=gbasedbt
pwd=GBase123
Servername=gbase01
CursorBehavior=0
CLIENT_LOCALE=zh_CN.utf8
DB_LOCALE=zh_CN.utf8
GL_USEGLU=1
TRANSLATIONDLL=/opt/gbase/lib/esql/igo4a304.so
;
; UNICODE connection Section
;
[ODBC]
;uncomment the below line for UNICODE connection
;UNICODE=UCS-4
;
; Trace file Section
;
Trace=0
TraceFile=/tmp/odbctrace.out
InstallDir=/opt/gbase
TRACEDLL=idmrs09a.so

2.2 验证ODBC配置

加载GBase 8s CSDK环境变量,包含连接到GBase 8s数据库所需的GBASEDBTDIR、LD_LIBRARY_PATH,以及选用的字符集DB_LOCALE、CLIENT_LOCALE、LANG等。加载ODBCINI环境变量。环境变量内容可保存为配置文件,如:gbase8s_odbc_env,以供调用。

export GBASEDBTDIR=/opt/gbase                    # GBase 8s CSDK安装目录
# 数据库使用的字符集
export DB_LOCALE=zh_CN.utf8
export CLIENT_LOCALE=zh_CN.utf8
export GL_USEGLU=1

export LD_LIBRARY_PATH=$GBASEDBTDIR/lib:$GBASEDBTDIR/lib/cli:$GBASEDBTDIR/lib/esql:$LD_LIBRARY_PATH
# ODBCINI
export ODBCINI=/etc/odbc.ini

通过isql命令检查ODBC配置

[root@a01 ~]#  . gbase8s_odbc_env
[root@a01 ~]#  isql -v utf8
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL> select dbservername::varchar(40) from dual;
+-----------------------------------------+
|                                         |
+-----------------------------------------+
| gbase01                                 |
+-----------------------------------------+
SQLRowCount returns -1
1 rows fetched

3,直接pip3安装pyodbc

通过pip3 install安装pyodbc,将自动安装最新的pyodbc

[root@a01 ~]# pip3 install pyodbc

然后通过pip3 list查看已经安装的版本

[root@a01 ~]# pip3 list
pip (9.0.3)
pyodbc (4.0.30)
pytz (2020.1)
setuptools (39.2.0)

4,Python通过PYODBC连接GBase 8s数据库测试

编写测试脚本程序test_pyodbc.py,内容如下:

# !/usr/bin/python3
# filename: test_pyodbc.py

import sys
import pyodbc

# use DSN, need PWD key word.
conn = pyodbc.connect("DSN=utf8;PWD=GBase123")
# set connection encoding
conn.setencoding(encoding='UTF-8')

mycursor = conn.cursor()
mycursor.execute("drop table if exists tpyodbc")

mycursor.execute("create table tpyodbc(id serial, name varchar(60), sex varchar(10))")
 
mycursor.execute("insert into tpyodbc(name,sex) values(?,?)",'张三三三','男')
mycursor.execute("insert into tpyodbc(name,sex) values(?,?)",'韩美美','女')
conn.commit()

cursor1 = conn.cursor()
cursor1.execute('select id,name,sex from tpyodbc')
rows = cursor1.fetchall()

for i, (id, name, sex) in enumerate(rows):
    print("ID: %d \t姓名:%s\t性别: %s" % (id, str(name), str(sex)))

conn.close()
sys.exit(0)

python test_pyodbc.py执行并输出结果:

[gbasedbt@a01 py]$ python3 test_pyodbc.py 
ID: 1     姓名:张三三三    性别: 男
ID: 2     姓名:韩美美    性别: 女

Python通过pyodbc连接到GBase 8s数据库,测试成功。

标签: pyodbc, gbase 8s, GBase, python3, unixodbc, setencoding

添加新评论