首頁 > 軟體

使用sqlalchemy-gbasedbt連線GBase 8s資料庫的步驟詳解

2023-04-02 06:02:31

測試環境:

  • 作業系統: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資料庫內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com