"G" Moment: Performance Optimization and Application Tips for the GBase 8s Python Driver

Published on 2025-08-15

In today's data-driven world, Python, as a powerful programming language, is increasingly integrated with databases. GBase 8s provides Python driver support, allowing Python developers to easily interact with GBase 8s databases. This article walks you through connecting to GBase 8s using the Python driver—from installing the driver and writing code to running examples—so you can get started quickly.

 

Preparation: Installing Python and the Driver

1. Install Python

Make sure your system has any version of Python from 3.7 to 3.12 installed. You can check your Python version with the following command:

python --version

If it is not installed, download and install it from the official Python website.

2. Install the GBase 8s Python Driver

Installing the GBase 8s Python driver is straightforward. Simply run the following command:

python -m pip install gbase8sdb

Note: The Python driver is platform-specific. If you need downloads for multiple platforms, click GBase Python driver to download the version and platform you require. We also now support the Python web framework Django and the Python SQL toolkit SQLAlchemy—you can download them from the same location.

If you encounter errors during installation, your pip version may be outdated. Upgrade pip with the following command:

python -m pip install --upgrade pip

Then try installing the driver again.

 

Environment Setup: Loading Dynamic Library Files

Before running the Python script, ensure the system can locate the GBase 8s dynamic library files. Execute the following command to load the libraries:

export LD_LIBRARY_PATH=$GSDK_PATH/lib

Make sure $GSDK_PATH is the path where you installed the GBase 8s SDK.

 

Writing Code: Creating and Running a Python Script

1. Write the Python Script

Create a file named test.py and write the following code inside it:

import gbase8sdb
# Replace with your database connection info
server_name = 'gbase363'  # Instance name
db_name = 'testdb'         # Database name
host = '192.168.75.134'   # IP address
port = 9363                # Port
user = 'gbasedbt'         # Username
password = '******'     # Password
# Create a connection
dsn = gbase8sdb.makedsn(server_name=server_name, db_name=db_name, host=host, port=port)
conn = gbase8sdb.connect(user=user, password=password, dsn=dsn)
cursor = conn.cursor()
# Create a table
cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
id SERIAL,
name VARCHAR(200) NOT NULL,  
age INTEGER
)
""")
conn.commit()
# Insert data
cursor.execute("INSERT INTO users (name, age) VALUES (?, ?)", ("Alice", 25))
users = [("Bob", 30), ("Charlie", 22)]
cursor.executemany("INSERT INTO users (name, age) VALUES (?, ?)", users)
conn.commit()
# Query data
cursor.execute("SELECT * FROM users")
rows = cursor.fetchall()
for row in rows:    
print(row)
# Update data
cursor.execute("UPDATE users SET age = ? WHERE name = ?", (26, "Alice"))
conn.commit()
# Delete data
cursor.execute("DELETE FROM users WHERE name = ?", ("Bob",))
conn.commit()
# Close the connection
cursor.close()
conn.close()

2. Run the Script

After saving the file, run the script with the following command:

python test.py

If everything goes well, you will see the query results output by the script, as shown below:

(1, 'Alice', 25)(2, 'Bob', 30)(3, 'Charlie', 22)

 

Notes

1. Server version: Make sure your GBase 8s server version is 3.6.2 or higher.

2. GSDK version: Make sure your GBase 8s GSDK version is 1.1 or higher.

3. Connection info: Before running the script, be sure to replace the connection information in the script with your actual database details.

Through this article, we believe you now have a preliminary understanding of how to use GBase 8s in a Python environment. From installing the driver and writing code to running examples, every step is clearly laid out. We hope this content helps you develop Python applications based on GBase 8s more efficiently.