Test environment:
- Operating system: CentOS 7.9 64-bit
- Database version: GBase8sV8.8_AEE_3.0.0_1, the corresponding CSDK version is 3.0.0_1
1. Confirm to install python3
Confirm that python3 and python3-devel are installed
[root@localhost test]# python3 -V Python 3.6.8
If it is not installed, it is recommended to use yum install python3 to install it.
Upgrade the pip version
[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 /packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3. (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 /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. Install the GBase 8s database connection tool (CSDK)
You can directly download the CSDK driver without installation:
Link: /s/1s9EW3VoRznlj6uDHubIEtg?pwd=ejfb
Extraction code: ejfb
Unzip to the specified directory /opt and generate the /opt/gbase8s-odbc-driver directory
[root@localhost test]# ll Total usage 35188 -rw-r--r--. 1 root root 36029237 3moon 11 20:52 GBase8s_3.0.0_1 [root@localhost test]# tar -zxf GBase8s_3.0.0_1 -C /opt/ [root@localhost test]# cd /opt/ [root@localhost opt]# ll Total usage 4 drwxr-xr-x. 20 gbasedbt gbasedbt 4096 3moon 10 15:14 gbase drwxrwxr-x. 9 1001 1003 88 12moon 13 2020 gbase8s-odbc-driver drwxr-xr-x. 2 root root 6 10moon 31 2018 rh
Create necessary environment variables and make the environment effective
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
Create sqlhosts configuration file
[root@localhost test]# vi /opt/gbase8s-odbc-driver/etc/sqlhosts [root@localhost test]# more /opt/gbase8s-odbc-driver/etc/sqlhosts gbase01 onsoctcp 9088
3. Install sqlalchemy-gbasedbt
3.1, Install sqlalchemy-gbasedbt online
Confirm that python3, python3-devel and gcc are all installed, CSDK has also been installed, and the environment variables have been configured, and can be directly connected to the network, you can use pip3 install sqlalchemy-gbasedbt to install directly.
[root@localhost test]# pip3 install sqlalchemy-gbasedbt Collecting sqlalchemy-gbasedbt Using cached sqlalchemy_gbasedbt-0.2. (10 kB) Collecting DbtPy Using cached DbtPy-3.0.5. (162 kB) Preparing metadata () ... 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 ' install' for DbtPy, since package 'wheel' is not installed. Installing collected packages: DbtPy, sqlalchemy-gbasedbt Running install for DbtPy ... done Successfully installed DbtPy-3.0.5.4 sqlalchemy-gbasedbt-0.2.4
The dependency packages: sqlalchemy, greenlet, importlib-metadata, typing-extensions, zipp and DbtPy will be installed at the same time. The installed pip3 list is as follows:
[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. Write a test demo and execute a test
Test demo files
#!/usr/bin/env python3 # Filename: testSqlalchemy_gbasedbt from sqlalchemy import MetaData, Table, Column, String, create_engine from import registry from import sessionmaker from import declarative_base ("gbasedbt", "sqlalchemy_gbasedbt.dbtdb", "GBasedbtDialect") # Create the base class of the object:Base = declarative_base() # Define User object:class User(Base): # Table name: __tablename__ = 'user' # Table structure: id = Column(String(20), primary_key=True) name = Column(String(20)) # Initialize the database connection:# ConStr = 'gbasedbt://<username>:<password>@<host name>:<port number>/<databasename>;SERVER=<server name>' ConStr = 'gbasedbt://gbasedbt:GBase123@:9088/testdb;SERVER=gbase01;DB_LOCALE=zh_CN.utf8;CLIENT_LOCALE=zh_CN.utf8;DELIMIDENT=y' engine = create_engine(ConStr) # Create an object.create_all(engine) # Create a DBSession type:DBSession = sessionmaker(bind=engine) # Create a session object:session = DBSession() # Create a new User object:new_user = User(id='2', name='Test the user') # Add to session:(new_user) # Submit and save to the database:() # Close session:() # Create Session:session = DBSession() # Create a Query query, filter is where condition, and finally call one() to return the only row, and if all() is called, return all rows:user = (User).filter(=='2').one() # Print type and object name attributes:print('type:', type(user)) print('name:', ) # Close Session:()
Test results:
[root@localhost test]# ./testSqlalchemy_gbasedbt.py
type: <class '__main__.User'>
name: Test user
This is the article about using sqlalchemy-gbasedbt to connect to GBase 8s database. For more related contents of sqlalchemy-gbasedbt to connect to GBase 8s database, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!