使用sqlalchemy-gbasedbt連接GBase 8s數(shù)據(jù)庫的步驟詳解
測試環(huán)境:
- 操作系統(tǒng):CentOS 7.9 64-bit
- 數(shù)據(jù)庫版本:GBase8sV8.8_AEE_3.0.0_1,對應(yīng)的CSDK版本為3.0.0_1
1,確認(rèn)安裝python3
確認(rèn)已經(jīng)安裝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數(shù)據(jù)庫連接工具(CSDK)
可以直接下載免安裝版本的CSDK驅(qū)動:
鏈接: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
創(chuàng)建必須的環(huán)境變量,并使環(huán)境生效
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
創(chuàng)建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
確認(rèn)python3、python3-devel和gcc均已經(jīng)安裝,CSDK也已經(jīng)安裝以及環(huán)境變量已經(jīng)配置的情況下,可直連網(wǎng)絡(luò)的情況下,可使用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,執(zhí)行測試
測試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") # 創(chuàng)建對象的基類: Base = declarative_base() # 定義User對象: class User(Base): # 表的名字: __tablename__ = 'user' # 表的結(jié)構(gòu): id = Column(String(20), primary_key=True) name = Column(String(20)) # 初始化數(shù)據(jù)庫連接: # 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) # 創(chuàng)建對象 Base.metadata.create_all(engine) # 創(chuàng)建DBSession類型: DBSession = sessionmaker(bind=engine) # 創(chuàng)建session對象: session = DBSession() # 創(chuàng)建新User對象: new_user = User(id='2', name='測試用戶') # 添加到session: session.add(new_user) # 提交即保存到數(shù)據(jù)庫: session.commit() # 關(guān)閉session: session.close() # 創(chuàng)建Session: session = DBSession() # 創(chuàng)建Query查詢,filter是where條件,最后調(diào)用one()返回唯一行,如果調(diào)用all()則返回所有行: user = session.query(User).filter(User.id=='2').one() # 打印類型和對象的name屬性: print('type:', type(user)) print('name:', user.name) # 關(guān)閉Session: session.close()
測試結(jié)果:
[root@localhost test]# ./testSqlalchemy_gbasedbt.py
type: <class '__main__.User'>
name: 測試用戶
到此這篇關(guān)于使用sqlalchemy-gbasedbt連接GBase 8s數(shù)據(jù)庫的文章就介紹到這了,更多相關(guān)sqlalchemy-gbasedbt連接GBase 8s數(shù)據(jù)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
數(shù)據(jù)庫 SQL千萬級數(shù)據(jù)規(guī)模處理概要
我在前年遇到過過億條的數(shù)據(jù)。以至于一個處理過程要幾個小時的。后面慢慢優(yōu)化,查找一些經(jīng)驗文章。才學(xué)到了一些基本方法。綜合敘之,與君探討之。2009-07-07解決mac上Navicat新建數(shù)據(jù)庫3680錯誤問題
這篇文章主要介紹了mac上Navicat新建數(shù)據(jù)庫3680錯誤解決辦法,很多朋友遇到這個問題不知道該如何解決,網(wǎng)上一搜一大把,但是不能解決核心問題,下面小編把我的解決過程分享給大家,需要的朋友可以參考下2021-11-11Sql Server 和 Access 操作數(shù)據(jù)庫結(jié)構(gòu)Sql語句小結(jié)
Sql Server 和 Access 操作數(shù)據(jù)庫結(jié)構(gòu)Sql語句小結(jié)...2007-06-06如何利用分析函數(shù)改寫范圍判斷自關(guān)聯(lián)查詢詳解
這篇文章主要給大家介紹了關(guān)于如何利用分析函數(shù)改寫范圍判斷自關(guān)聯(lián)查詢的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用sql具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10postgres 數(shù)據(jù)庫中的數(shù)據(jù)轉(zhuǎn)換
postgres8.3以后,字段數(shù)據(jù)之間的默認(rèn)轉(zhuǎn)換取消了。如果需要進(jìn)行數(shù)據(jù)變換的話,在postgres數(shù)據(jù)庫中,我們可以用"::"來進(jìn)行字段數(shù)據(jù)的類型轉(zhuǎn)換。2009-07-07關(guān)系型數(shù)據(jù)庫的設(shè)計規(guī)則詳解
大家好,本篇文章主要講的是關(guān)系型數(shù)據(jù)庫的設(shè)計規(guī)則詳解,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12關(guān)于Navicat連接MySql數(shù)據(jù)庫慢的問題
這篇文章主要介紹了關(guān)于Navicat連接MySql數(shù)據(jù)庫慢的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03