怎么使用sqlalchemy-gbasedbt连接GBase?8s数据库


这篇“怎么使用sqlalchemy-gbasedbt连接GBase8s数据库”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“怎么使用sqlalchemy-gbasedbt连接GBase8s数据库”文章吧。

    测试环境:

    • 操作系统:CentOS 7.9 64-bit

    • 数据库版本:GBase8sV8.8_AEE_3.0.0_1,对应的CSDK版本为3.0.0_1

    1,确认安装python3

    确认已经安装python3和python3-devel

    [root@localhosttest]#python3-VPython3.6.8

    如果没有安装,建议使用yum install python3来安装。
    升级pip的版本

    [root@localhosttest]#python3-mpipinstall--upgrade--force-reinstallpipWARNING:Runningpipinstallwithrootprivilegesisgenerallynotagoodidea.Try`__main__.pyinstall--user`instead.CollectingpipDownloadingfiles.pythonhosted.org/packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl(1.7MB)100%|████████████████████████████████|1.7MB235kB/sInstallingcollectedpackages:pipSuccessfullyinstalledpip-21.3.1[root@localhosttest]#pip3listWARNING:pipisbeinginvokedbyanoldscriptwrapper.Thiswillfailinafutureversionofpip.Pleaseseegithub/pypa/pip/issues/5599foradviceonfixingtheunderlyingissue.ToavoidthisproblemyoucaninvokePythonwith'-mpip'insteadofrunningpipdirectly.PackageVersion-----------------pip21.3.1setuptools39.2.0

    2,安装GBase 8s数据库连接工具(CSDK)


    解压到指定目录/opt下,生成/opt/gbase8s-odbc-driver目录

    [root@localhosttest]#ll总用量35188-rw-r--r--.1rootroot360292373月1120:52GBase8s_3.0.0_1-Linux64-ODBC-Driver.tar.gz[root@localhosttest]#tar-zxfGBase8s_3.0.0_1-Linux64-ODBC-Driver.tar.gz-C/opt/[root@localhosttest]#cd/opt/[root@localhostopt]#ll总用量4drwxr-xr-x.20gbasedbtgbasedbt40963月1015:14gbasedrwxrwxr-x.9100110038812月132023gbase8s-odbc-driverdrwxr-xr-x.2rootroot610月312023rh

    创建必须的环境变量,并使环境生效

    exportGBASEDBTDIR=/opt/gbase8s-odbc-driverexportCSDK_HOME=/opt/gbase8s-odbc-driverexportPATH=$GBASEDBTDIR/bin:$PATHexportLD_LIBRARY_PATH=$GBASEDBTDIR/lib:$GBASEDBTDIR/lib/cli:$GBASEDBTDIR/lib/esql:$LD_LIBRARY_PATH

    创建sqlhosts配置文件

    [root@localhosttest]#vi/opt/gbase8s-odbc-driver/etc/sqlhosts[root@localhosttest]#more/opt/gbase8s-odbc-driver/etc/sqlhostsgbase01onsoctcpa02.gbasedbt9088

    3,安装sqlalchemy-gbasedbt

    3.1, 在线安装sqlalchemy-gbasedbt

    确认python3、python3-devel和gcc均已经安装,CSDK也已经安装以及环境变量已经配置的情况下,可直连网络的情况下,可使用pip3 install sqlalchemy-gbasedbt直接安装

    [root@localhosttest]#pip3installsqlalchemy-gbasedbtCollectingsqlalchemy-gbasedbtUsingcachedsqlalchemy_gbasedbt-0.2.4-py3-none-any.whl(10kB)CollectingDbtPyUsingcachedDbtPy-3.0.5.4.tar.gz(162kB)Preparingmetadata(setup.py)...doneRequirementalreadysatisfied:sqlalchemyin/usr/local/lib64/python3.6/site-packages(fromsqlalchemy-gbasedbt)(1.4.46)Requirementalreadysatisfied:greenlet!=0.4.17in/usr/local/lib64/python3.6/site-packages(fromsqlalchemy->sqlalchemy-gbasedbt)(2.0.2)Requirementalreadysatisfied:importlib-metadatain/usr/local/lib/python3.6/site-packages(fromsqlalchemy->sqlalchemy-gbasedbt)(4.8.3)Requirementalreadysatisfied:typing-extensions>=3.6.4in/usr/local/lib/python3.6/site-packages(fromimportlib-metadata->sqlalchemy->sqlalchemy-gbasedbt)(4.1.1)Requirementalreadysatisfied:zipp>=0.5in/usr/local/lib/python3.6/site-packages(fromimportlib-metadata->sqlalchemy->sqlalchemy-gbasedbt)(3.6.0)Usinglegacy'setup.pyinstall'forDbtPy,sincepackage'wheel'isnotinstalled.Installingcollectedpackages:DbtPy,sqlalchemy-gbasedbtRunningsetup.pyinstallforDbtPy...doneSuccessfullyinstalledDbtPy-3.0.5.4sqlalchemy-gbasedbt-0.2.4

    将同时安装依赖包:sqlalchemy、greenlet、importlib-metadata、typing-extensions、zipp和DbtPy,安装后的pip3列表如下:

    [root@localhosttest]#pip3listPackageVersion--------------------------DbtPy3.0.5.4greenlet2.0.2importlib-metadata4.8.3pip21.3.1setuptools39.2.0SQLAlchemy1.4.46sqlalchemy-gbasedbt0.2.4typing_extensions4.1.1zipp3.6.0

    4,编写测试Demo,执行测试

    测试demo文件

    #!/usr/bin/envpython3#Filename:testSqlalchemy_gbasedbtfromsqlalchemyimportMetaData,Table,Column,String,create_enginefromsqlalchemy.dialectsimportregistryfromsqlalchemy.ormimportsessionmakerfromsqlalchemy.ext.declarativeimportdeclarative_baseregistry.register("gbasedbt","sqlalchemy_gbasedbt.dbtdb","GBasedbtDialect")#创建对象的基类:Base=declarative_base()#定义User对象:classUser(Base):#表的名字:__tablename__='user'#表的结构:id=Column(String(20),primary_key=True)name=Column(String(20))#初始化数据库连接:#ConStr='gbasedbt://<username>:<password>@<hostname>:<portnumber>/<databasename>;SERVER=<servername>'ConStr='gbasedbt://gbasedbt:GBase123@a02.gbasedbt:9088/testdb;SERVER=gbase01;DB_LOCALE=zh.utf8;CLIENT_LOCALE=zh.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)#提交即保存到数据库:sessionmit()#关闭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连接GBase8s数据库”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注主机评测网行业资讯频道。


    上一篇:Spring?MVC启动之HandlerMapping作用及实现方法是什么

    下一篇:Mybatis怎么实现一对一、一对多关联查询


    Copyright © 2002-2019 测速网 https://www.inhv.cn/ 皖ICP备2023010105号 城市 地区 街道
    温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!
    热门搜索