Linux下Python3通过DbtPy连接到GBase 8s数据库
操作系统:CentOS 7.8 64-bit
软件需求:
Python 3.6.8 及Python-devel 3.6.8
有互联网连接的情况下,可以直接安装DbtPy (仅限Linux x64和Win x64)
1,直接pip3安装DbtPy,将自动安装最新的DbtPy
[root@a02 ~]# pip3 install DbtPy
Collecting DbtPy
Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/1f/45/22d842ffe40fbcb58878b891ba8f4e319a3585afd0f09ac13fd3d51b0a9b/DbtPy-3.0.5.tar.gz (165kB)
100% |████████████████████████████████| 174kB 13.0MB/s
# 注:对于Linux x64和Win x64,这步可以需要些时间下载对应的csdk驱动。
Installing collected packages: DbtPy
Running setup.py install for DbtPy ... done
Successfully installed DbtPy-3.0.5
2,然后通过pip3 list查看已经安装的版本
[root@a02 ~]# pip3 list | grep DbtPy
DbtPy (3.0.5)
3,设置环境变量
[root@a02 ~]# export GBASEDBTDIR=/usr/local/lib64/python3.6/site-packages/gbase8s-odbc-driver
[root@a02 ~]# export LD_LIBRARY_PATH=$GBASEDBTDIR/lib:$GBASEDBTDIR/lib/cli:$GBASEDBTDIR/lib/esql:$LD_LIBRARY_PATH
[root@a02 ~]# unset ODBCINI
注:GBASEDBTDIR的路径与python的版本有关,请注意修改。
ODBCINI环境变量需要去除。
4,Python3通过DbtPy连接GBase 8s数据库测试
编写测试脚本程序TestDbtPy.py,内容如下:
#!/usr/bin/python3
# filename: TestDbtPy.py
import sys
import DbtPy
print("Python DbtPy测试程序开始运行.\n")
connectStr="PROTOCOL=onsoctcp;HOST=a02.gbasedbt.com;SERVICE=9088;SERVER=gbase01;DATABASE=testdb;DB_LOCALE=zh_CN.utf8;CLIENT_LOCALE=zh_CN.utf8"
conn=DbtPy.connect(connectStr, "gbasedbt", "GBase123")
DbtPy.exec_immediate(conn, "drop table if exists company")
DbtPy.exec_immediate(conn, "create table company(coid serial,coname varchar(255),coaddr varchar(255))")
DbtPy.exec_immediate(conn, "insert into company(coname,coaddr) values ('南大通用','天津市普天创新园')")
DbtPy.exec_immediate(conn, "insert into company(coname,coaddr) values ('南大通用北京分公司','北京市朝阳区太阳宫')")
stmt = DbtPy.exec_immediate(conn, "select * from company")
data = DbtPy.fetch_assoc(stmt)
rc = 0
while data != False:
rc += 1
print (data)
data = DbtPy.fetch_assoc(stmt)
DbtPy.free_result(stmt)
DbtPy.free_stmt(stmt)
DbtPy.close(conn)
print("\nPython DbtPy测试程序结束运行.")
sys.exit(0)
python TestDbtPy.py执行并输出结果:
[root@a02 ~]# python3 TestDbtPy.py
Python DbtPy测试程序开始运行.
{'coid': 1, 'coname': '南大通用', 'coaddr': '天津市普天创新园'}
{'coid': 2, 'coname': '南大通用北京分公司', 'coaddr': '北京市朝阳区太阳宫'}
Python DbtPy测试程序结束运行.
如果是内网环境,无法通过互联网连接。
1,从互联网上下载DbtPy源码包和CSDK安装包(或者自己手工安装CSDK)
[root@a02 ~]# wget https://gbasedbt.com/odbc/GBase8s_3.0-Linux64-ODBC-Driver.tar.gz
--2021-04-02 09:57:30-- https://gbasedbt.com/odbc/GBase8s_3.0-Linux64-ODBC-Driver.tar.gz
Resolving gbasedbt.com (gbasedbt.com)... 106.55.146.212
Connecting to gbasedbt.com (gbasedbt.com)|106.55.146.212|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 36029201 (34M) [application/x-gzip]
Saving to: ‘GBase8s_3.0-Linux64-ODBC-Driver.tar.gz’
100%[==========================================================================================================>] 36,029,201 637KB/s in 56s
2021-04-02 09:58:26 (627 KB/s) - ‘GBase8s_3.0-Linux64-ODBC-Driver.tar.gz’ saved [36029201/36029201]
[root@a02 ~]# wget https://gbasedbt.com/dl/DbtPy/DbtPy-3.0.5.tar.gz
--2021-04-02 09:59:44-- https://gbasedbt.com/dl/DbtPy/DbtPy-3.0.5.tar.gz
Resolving gbasedbt.com (gbasedbt.com)... 106.55.146.212
Connecting to gbasedbt.com (gbasedbt.com)|106.55.146.212|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 172254 (168K) [application/x-gzip]
Saving to: ‘DbtPy-3.0.5.tar.gz’
100%[==========================================================================================================>] 172,254 --.-K/s in 0.03s
2021-04-02 09:59:44 (6.30 MB/s) - ‘DbtPy-3.0.5.tar.gz’ saved [172254/172254]
2,解压GBase8s_3.0-Linux64-ODBC-Driver.tar.gz,并配置环境变量
[root@a02 ~]# tar -zxf GBase8s3.0.0_1-Linux64-ODBC-Driver.tar.gz -C /opt/
将在指定的目录/opt下生成gbase8s-odbc-driver目录
配置环境变量,包含GBASEDBTDIR、CSDK_HOME和LD_LIBRARY_PATH
export GBASEDBTDIR=/opt/gbase8s-odbc-driver
export CSDK_HOME=$GBASEDBTDIR
export LD_LIBRARY_PATH=$CSDK_HOME/lib:$CSDK_HOME/lib/cli:$CSDK_HOME/lib/esql:$LD_LIBRARY_PATH
unset ODBCINI
注:ODBCINI环境变量需要去除。
3,手动安装DbtPy
解压缩DbtPy-3.0.5.tar.gz
[root@a02 ~]# tar -zxvf DbtPy-3.0.5.tar.gz && cd DbtPy-3.0.5
执行安装
[root@a02 DbtPy-3.0.5]# python3 setup.py install
Detected 64-bit Python
Smart Triggers are not available.
running install
running bdist_egg
running egg_info
writing DbtPy.egg-info/PKG-INFO
writing dependency_links to DbtPy.egg-info/dependency_links.txt
writing top-level names to DbtPy.egg-info/top_level.txt
reading manifest file 'DbtPy.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no files found matching '*.pyd'
writing manifest file 'DbtPy.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
creating build
creating build/lib.linux-x86_64-3.6
copying DbtPyDbi.py -> build/lib.linux-x86_64-3.6
(省略)
creating dist
creating 'dist/DbtPy-3.0.5-py3.6-linux-x86_64.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing DbtPy-3.0.5-py3.6-linux-x86_64.egg
creating /usr/local/lib64/python3.6/site-packages/DbtPy-3.0.5-py3.6-linux-x86_64.egg
Extracting DbtPy-3.0.5-py3.6-linux-x86_64.egg to /usr/local/lib64/python3.6/site-packages
Adding DbtPy 3.0.5 to easy-install.pth file
Installed /usr/local/lib64/python3.6/site-packages/DbtPy-3.0.5-py3.6-linux-x86_64.egg
Processing dependencies for DbtPy==3.0.5
Finished processing dependencies for DbtPy==3.0.5
4,然后通过pip3 list查看已经安装的版本
[root@a02 ~]# pip3 list | grep DbtPy
DbtPy (3.0.5)
5,Python3通过DbtPy连接GBase 8s数据库测试
编写测试脚本程序TestDbtPy.py,内容如下:
#!/usr/bin/python3
# filename: TestDbtPy.py
import sys
import DbtPy
print("Python DbtPy测试程序开始运行.\n")
connectStr="PROTOCOL=onsoctcp;HOST=a02.gbasedbt.com;SERVICE=9088;SERVER=gbase01;DATABASE=testdb;DB_LOCALE=zh_CN.utf8;CLIENT_LOCALE=zh_CN.utf8"
conn=DbtPy.connect(connectStr, "gbasedbt", "GBase123")
DbtPy.exec_immediate(conn, "drop table if exists company")
DbtPy.exec_immediate(conn, "create table company(coid serial,coname varchar(255),coaddr varchar(255))")
DbtPy.exec_immediate(conn, "insert into company(coname,coaddr) values ('南大通用','天津市普天创新园')")
DbtPy.exec_immediate(conn, "insert into company(coname,coaddr) values ('南大通用北京分公司','北京市朝阳区太阳宫')")
stmt = DbtPy.exec_immediate(conn, "select * from company")
data = DbtPy.fetch_assoc(stmt)
rc = 0
while data != False:
rc += 1
print (data)
data = DbtPy.fetch_assoc(stmt)
DbtPy.free_result(stmt)
DbtPy.free_stmt(stmt)
DbtPy.close(conn)
print("\nPython DbtPy测试程序结束运行.")
sys.exit(0)
python TestDbtPy.py执行并输出结果:
[root@a02 ~]# python3 TestDbtPy.py
Python DbtPy测试程序开始运行.
{'coid': 1, 'coname': '南大通用', 'coaddr': '天津市普天创新园'}
{'coid': 2, 'coname': '南大通用北京分公司', 'coaddr': '北京市朝阳区太阳宫'}
Python DbtPy测试程序结束运行.