测试环境:
- 操作系统:centos 7.9 64-bit
- 数据库版本:gbase8sv8.8_aee_3.0.0_1,对应的csdk版本为3.0.0_1
1,确认安装python3
确认已经安装python3和python3-devel
[root@localhost test]# python3 -v python 3.6.8
如果没有安装,建议使用yum install python3来安装。
升级pip的版本
[root@localhost test]# python3 -m pip install --upgrade --force-reinstall pip warning: running pip install with root privileges is generally not a good idea. try `__main__.py install --user` instead. collecting pip downloading https://files.pythonhosted.org/packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl (1.7mb) 100% |████████████████████████████████| 1.7mb 235kb/s installing collected packages: pip successfully installed pip-21.3.1 [root@localhost test]# pip3 list warning: pip is being invoked by an old script wrapper. this will fail in a future version of pip. please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue. to avoid this problem you can invoke python with '-m pip' instead of running pip directly. package version ---------- ------- pip 21.3.1 setuptools 39.2.0
2,安装gbase 8s数据库连接工具(csdk)
可以直接下载免安装版本的csdk驱动:
链接:https://pan.baidu.com/s/1s9ew3vorznlj6udhubietg?pwd=ejfb
提取码:ejfb
解压到指定目录/opt下,生成/opt/gbase8s-odbc-driver目录
[root@localhost test]# ll 总用量 35188 -rw-r--r--. 1 root root 36029237 3月 11 20:52 gbase8s_3.0.0_1-linux64-odbc-driver.tar.gz [root@localhost test]# tar -zxf gbase8s_3.0.0_1-linux64-odbc-driver.tar.gz -c /opt/ [root@localhost test]# cd /opt/ [root@localhost opt]# ll 总用量 4 drwxr-xr-x. 20 gbasedbt gbasedbt 4096 3月 10 15:14 gbase drwxrwxr-x. 9 1001 1003 88 12月 13 2020 gbase8s-odbc-driver drwxr-xr-x. 2 root root 6 10月 31 2018 rh
创建必须的环境变量,并使环境生效
export gbasedbtdir=/opt/gbase8s-odbc-driver export csdk_home=/opt/gbase8s-odbc-driver export path=$gbasedbtdir/bin:$path export ld_library_path=$gbasedbtdir/lib:$gbasedbtdir/lib/cli:$gbasedbtdir/lib/esql:$ld_library_path
创建sqlhosts配置文件
[root@localhost test]# vi /opt/gbase8s-odbc-driver/etc/sqlhosts [root@localhost test]# more /opt/gbase8s-odbc-driver/etc/sqlhosts gbase01 onsoctcp a02.gbasedbt.com 9088
3,安装sqlalchemy-gbasedbt
3.1, 在线安装sqlalchemy-gbasedbt
确认python3、python3-devel和gcc均已经安装,csdk也已经安装以及环境变量已经配置的情况下,可直连网络的情况下,可使用pip3 install sqlalchemy-gbasedbt直接安装
[root@localhost test]# pip3 install sqlalchemy-gbasedbt collecting sqlalchemy-gbasedbt using cached sqlalchemy_gbasedbt-0.2.4-py3-none-any.whl (10 kb) collecting dbtpy using cached dbtpy-3.0.5.4.tar.gz (162 kb) preparing metadata (setup.py) ... done requirement already satisfied: sqlalchemy in /usr/local/lib64/python3.6/site-packages (from sqlalchemy-gbasedbt) (1.4.46) requirement already satisfied: greenlet!=0.4.17 in /usr/local/lib64/python3.6/site-packages (from sqlalchemy->sqlalchemy-gbasedbt) (2.0.2) requirement already satisfied: importlib-metadata in /usr/local/lib/python3.6/site-packages (from sqlalchemy->sqlalchemy-gbasedbt) (4.8.3) requirement already satisfied: typing-extensions>=3.6.4 in /usr/local/lib/python3.6/site-packages (from importlib-metadata->sqlalchemy->sqlalchemy-gbasedbt) (4.1.1) requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.6/site-packages (from importlib-metadata->sqlalchemy->sqlalchemy-gbasedbt) (3.6.0) using legacy 'setup.py install' for dbtpy, since package 'wheel' is not installed. installing collected packages: dbtpy, sqlalchemy-gbasedbt running setup.py install for dbtpy ... done successfully installed dbtpy-3.0.5.4 sqlalchemy-gbasedbt-0.2.4
将同时安装依赖包:sqlalchemy、greenlet、importlib-metadata、typing-extensions、zipp和dbtpy,安装后的pip3列表如下:
[root@localhost test]# pip3 list package version ------------------- ------- dbtpy 3.0.5.4 greenlet 2.0.2 importlib-metadata 4.8.3 pip 21.3.1 setuptools 39.2.0 sqlalchemy 1.4.46 sqlalchemy-gbasedbt 0.2.4 typing_extensions 4.1.1 zipp 3.6.0
4,编写测试demo,执行测试
测试demo文件
#!/usr/bin/env python3 # filename: testsqlalchemy_gbasedbt from sqlalchemy import metadata, table, column, string, create_engine from sqlalchemy.dialects import registry from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base registry.register("gbasedbt", "sqlalchemy_gbasedbt.dbtdb", "gbasedbtdialect") # 创建对象的基类: base = declarative_base() # 定义user对象: class user(base): # 表的名字: __tablename__ = 'user' # 表的结构: id = column(string(20), primary_key=true) name = column(string(20)) # 初始化数据库连接: # constr = 'gbasedbt://<username>:<password>@<host name>:<port number>/<databasename>;server=<server name>' constr = 'gbasedbt://gbasedbt:gbase123@a02.gbasedbt.com:9088/testdb;server=gbase01;db_locale=zh_cn.utf8;client_locale=zh_cn.utf8;delimident=y' engine = create_engine(constr) # 创建对象 base.metadata.create_all(engine) # 创建dbsession类型: dbsession = sessionmaker(bind=engine) # 创建session对象: session = dbsession() # 创建新user对象: new_user = user(id='2', name='测试用户') # 添加到session: session.add(new_user) # 提交即保存到数据库: session.commit() # 关闭session: session.close() # 创建session: session = dbsession() # 创建query查询,filter是where条件,最后调用one()返回唯一行,如果调用all()则返回所有行: user = session.query(user).filter(user.id=='2').one() # 打印类型和对象的name属性: print('type:', type(user)) print('name:', user.name) # 关闭session: session.close()
测试结果:
[root@localhost test]# ./testsqlalchemy_gbasedbt.py
type: <class '__main__.user'>
name: 测试用户
到此这篇关于使用sqlalchemy-gbasedbt连接gbase 8s数据库的文章就介绍到这了,更多相关sqlalchemy-gbasedbt连接gbase 8s数据库内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论