This topic introduces how to build an application by using mysqlclient and OceanBase Database. It also covers the use of the application for fundamental database operations, including table creation, data insertion, and data query.
Prerequisites
- You have installed Python 3.x and pip.
- You have installed OceanBase Database and created a MySQL tenant.
Procedure
- Check the versions of Python and pip.
- Install the
mysqlclientlibrary. - Obtain the connection information of OceanBase Database.
- Modify the database connection information in the
config.pyfile. - Run the
main.pyfile. - Perform database operations in the CLI.
Step 1: Check the versions of Python and pip
Open Command Prompt or PowerShell, and run the python --version and pip --version commands to ensure that Python and pip are properly installed.
Here is an example:
PS C:\Windows\system32> python --version
Python 3.11.2
PS C:\Windows\system32> pip --version
pip 23.3.1 from C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\pip (python 3.11)
Step 2: Install the mysqlclient library
When you install the mysqlclient library, you must compile and link the MySQL C API to connect to OceanBase Database. Therefore, you must install the development files of MySQL Connector C or MySQL C API for later compilation.
Method 1: Install the mysqlclient library by using a precompiled binary file
In a Windows environment, you can install the mysqlclient library using precompiled binary files. Download the whl file that is compatible with your Python version and operating system platform from Download files and use the pip install command to install it.
Here is an example:
Open Command Prompt or PowerShell, and run the following commands to install the mysqlclient library.
Navigate to the directory where the
whlfile is located.cd D:\downloadRun the following command to install the
mysqlclientlibrary:PS D:\download> pip install mysqlclient-2.2.0-cp311-cp311-win_amd64.whl Processing d:\download\mysqlclient-2.2.0-cp311-cp311-win_amd64.whl Installing collected packages: mysqlclient Successfully installed mysqlclient-2.2.0
Method 2: Install the mysqlclient library directly
Install MySQL.
Before you use mysqlclient, you need to install the MySQL database server. mysqlclient depends on the client program and library files of MySQL to communicate with MySQL. You can download the installation program from the MySQL official website and install it as prompted.
Install a C compiler.
In a Windows environment, you can choose to install Visual Studio or MinGW as the C compiler.
Install a development tool.
In a Windows environment, you can use MySQL Connector/C to obtain the MySQL development libraries. To install MySQL Connector/C, perform the following steps:
Go to the MySQL Community Downloads page.
On this page, select the appropriate operating system type and version, and download the corresponding installation package.
Run the downloaded installation package and follow the instructions in the installation wizard.
During the installation, select the Custom installation type and ensure that Development Components are installed.
After the installation is completed, restart your computer.
Install the mysqlclient library.
Open Command Prompt or PowerShell, and run the following commands to install the mysqlclient library.
Run the following command to go to the
python-mysqlclientproject directory:cd python-mysqlclientRun the following command to install the mysqlclient library:
pip install -r requirements.txt
Note
You can also directly run the
pip install mysqlclientcommand to install themysqlclientlibrary.
In a Linux environment, Python C extensions rely on the Python.h header file, which is usually contained in the python3-devel package. If the python3-devel package is not installed, an error will be returned when you compile the MySQL C API, indicating that the Python.h header file cannot be found.
To resolve this issue, open the command prompt or PowerShell terminal and run the following commands to install the necessary Python libraries.
Check whether the
python3-develpackage is installed.Run the following command to check whether the
python3-develpackage is installed:rpm -q python3-develIf yes, the following information is displayed:
python3-devel-3.x.x-x.el7.x86_64Here,
3.x.xindicates the version of Python3, andx86_64indicates the CPU architecture of the system.If the package is not installed, the following information is displayed:
package python3-devel is not installedIn this case, you can run the following command to install the
python3-develpackage:sudo yum install python3-develAfter the installation is completed, run the
rpm -q python3-develcommand again to verify whether the installation is successful.
Install the mysqlclient library.
Run the following command to go to the
python-mysqlclientproject directory:cd python-mysqlclientRun the following command to install the Python library required by the project:
sudo pip install -r requirements.txt
Note
You can also run the
sudo pip install mysqlclientcommand to install the mysqlclient library.
Step 3: Obtain the connection information of OceanBase Database
Contact the deployment personnel or administrator of OceanBase Database to obtain the connection string.
obclient -h$host -P$port -u$user_name -p$password -D$database_name
where
$hostspecifies the IP address for connecting to OceanBase Database. For connection through OceanBase Database Proxy (ODP), use the IP address of an ODP. For direct connection, use the IP address of an OBServer node.$portspecifies the port for connecting to OceanBase Database. For connection through ODP, the default value is2883, which can be customized when ODP is deployed. For direct connection, the default value is2881, which can be customized when OceanBase Database is deployed.$database_namespecifies the name of the database to be accessed.Notice
The user for connecting to a tenant must have the
CREATE,INSERT, andSELECTprivileges on the database. For more information about user privileges, see Privilege types in MySQL mode.$user_namespecifies the username of the account in the format of user@tenant#cluster name or username@SERVICE:service name. It is specified by the-uparameter. When the value is in the user@tenant#cluster name format, the default tenant issysand the default administrator isroot. The cluster name is not required when you directly connect to OceanBase Database, but is required when you connect to OceanBase Database through ODP.$passwordspecifies the password of the account.
For more information about the connection string, see Connect to an OceanBase tenant by using OBClient.
Here is an example:
obclient -hxxx.xxx.xxx.xxx -P2881 -utest_user001@mysql001 -p****** -Dtest
Step 4: Modify the database connection information in the config.py file
Modify the database connection information in the config.py file in the python-mysqlclient/ directory based on the connection string obtained in Step 3: Obtain the connection information of OceanBase Database.
Go to the
python-mysqlclientproject directory.Modify the database connection information in the
config.pyfile.- In a Windows environment, use the text editor to open the
config.pyfile and modify the database connection information based on the actual situation. - In a Linux environment, use the
vi config.pyorvim config.pycommand to open theconfig.pyfile and modify the database connection information based on the actual situation.
Here is an example of the database connection information in the
config.pyfile:OCEANBASE_CONFIG = { 'host': '10.10.10.1', 'port': 2881, 'user': 'test_user001@mysql001', 'password': '******', 'database': 'test', 'charset': 'utf8mb4' }- In a Windows environment, use the text editor to open the
Step 5: Run the main.py file
Open Command Prompt or PowerShell, and run the python main.py command to start the application.
Go to the
python-mysqlclientproject directory.Here is an example:
cd /home/admin/python-mysqlclientRun the following command to start the
main.pyapplication:python main.pyThe return result is as follows:
Table created successfully Instruction: 1.Insert Data; 2.Query Data; 3.Exit. Enter the command [1/2/3]>
Step 6: Perform database operations on the CLI
Example of successful data insertion
On the CLI, enter
1and press Enter.Here is an example:
Enter the command [1/2/3]> 1Enter a name after the
Enter name:prompt and press Enter.Here is an example:
Enter name: A1Enter an age after the
Enter age:prompt and press Enter.Here is an example:
Enter age: 18The system displays
Record inserted successfully, indicating that the data is successfully inserted. Finally, an instruction is displayed, prompting you to enter1,2, or3and press Enter to perform the corresponding operation.Here is an example:
Record inserted successfully Instruction: 1.Insert Data; 2.Query Data; 3.Exit. Enter the command [1/2/3]>
Example of failed data insertion
On the CLI, enter
1and press Enter.Here is an example:
Enter the command [1/2/3]> 1Enter a name after the
Enter name:prompt and press Enter.Here is an example:
Enter name: A2Enter an age after the
Enter age:prompt and press Enter.Notice
The data type of the
agefield is INT.Here is an example:
Enter age: 18The system displays
(1366, 'Incorrect integer value'), indicating that the data failed to be inserted. Finally, an instruction is displayed, prompting you to enter1,2, or3and press Enter to perform the corresponding operation.Here is an example:
(1366, 'Incorrect integer value') Instruction: 1.Insert Data; 2.Query Data; 3.Exit. Enter the command [1/2/3]>
Example of data query
In the CLI, enter
2and press Enter.Here is an example:
Enter the command [1/2/3]> 2The data of the table is displayed. Finally, an instruction is displayed, prompting you to enter
1,2, or3and press Enter to perform the corresponding operation.Here is an example:
(1, 'A1', 18) Instruction: 1.Insert Data; 2.Query Data; 3.Exit. Enter the command [1/2/3]>
Example of entering an incorrect command
In the CLI, enter a value other than
1,2, and3, and press Enter.Here is an example:
Enter the command [1/2/3]> AThe following error message is displayed:
Invalid command, please enter command again [1/2/3]. Finally, an instruction is displayed, prompting you to enter1,2, or3and press Enter to perform the corresponding operation.Here is an example:
Invalid command, please enter command again [1/2/3] Instruction: 1.Insert Data; 2.Query Data; 3.Exit. Enter the command [1/2/3]>
Example of exiting the application
In the CLI, enter
3and press Enter.Here is an example:
Enter the command [1/2/3]> 3
Project code
Click here to download the project code, which is a package named python-mysqlclient.zip.
Decompress the package to obtain a folder named python-mysqlclient. The directory structure is as follows:
python-mysqlclient
├── config.py
├── db.py
├── main.py
└── requirements.txt
Here is a breakdown of the files and directories:
config.py: manages database connection configurations.db.py: operates the database, such as creating tables, inserting data, and querying data.main.py: serves as the entry to the application and provides a simple CLI. You can enter commands in the CLI to perform corresponding operations.requirements.txt: lists the Python libraries required for the project and their version requirements.Note
The
requirements.txtfile in the package obtained in this topic lists only the mysqlclient library and its version requirements. You can run thepip install -r requirements.txtcommand to automatically install the required library.
Code in config.py
When you connect to a database by using Python, you must specify database connection parameters. You can store the parameters in a separate configuration file such as config.py. You can encapsulate the parameters in a dictionary so that other Python files can reference the dictionary to connect to the database, without the need to write the parameters to each file.
Code in the config.py file obtained in this topic defines a dictionary variable named OCEANBASE_CONFIG, which is used to manage the connection parameters of OceanBase Database.
Here is the sample code:
OCEANBASE_CONFIG = {
'host': 'localhost',
'port': port,
'user': 'user_name',
'password': '',
'database': 'db_name',
'charset': 'utf8mb4'
}
where
hostspecifies the IP address for connecting to OceanBase Database. For connection through ODP, use the IP address of an ODP. For direct connection, use the IP address of an OBServer node.portspecifies the port for connecting to OceanBase Database. The default value is2881for direct connection and2883for connection through ODP.userspecifies the username for connecting to the database. It is in the format of {username}@{tenant name} for direct connection or in the format of {username}@{tenant name}#{cluster name} for connection through ODP. For more information about how to connect to OceanBase Database, see Connection methods.passwordspecifies the password for connecting to the database.databasespecifies the name of the database to connect to.charsetspecifies the character set for connecting to the database.
Notice
The actual parameter configurations depend on the project requirements and database characteristics. We recommend that you adjust and configure the parameters based on the actual situation.
Code in db.py
The db.py file is a Python module in which database operations are encapsulated. You can use it to add, delete, modify, and query data in a database.
To configure the db.py file, perform the following steps:
Import the MySQLdb module and database connection parameters.
Here is the sample code:
import MySQLdb from config import OCEANBASE_CONFIGDefine a function for creating a table.
- Define the
create_tablefunction to create a table namedtest_tbl1in OceanBase Database. - Use the
withstatement to manage the lifecycle of database connections and cursor objects. This ensures that database connections and cursor objects can be securely closed without memory leaks. - Define an SQL statement, execute the statement, and print the execution result or error message.
Here is the sample code:
def create_table(): with MySQLdb.connect(**OCEANBASE_CONFIG) as conn: with conn.cursor() as cursor: try: create_table_sql = """ CREATE TABLE test_tbl1 ( id INT UNSIGNED NOT NULL AUTO_INCREMENT, name VARCHAR(50) NOT NULL, age INT UNSIGNED NOT NULL, PRIMARY KEY (id) ) ENGINE=OCEANBASE AUTO_INCREMENT=1 """ cursor.execute(create_table_sql) print("Table created successfully") except MySQLdb.Error as err: print(err)- Define the
Define a function for inserting data.
- Define the
insert_recordfunction to insert a record into a specified table. The record contains thenameandagefields. - Use the
withstatement to manage the lifecycle of database connections and cursor objects. This ensures that database connections and cursor objects can be securely closed without memory leaks. - Define an SQL statement, execute the statement, commit the transaction, and print the execution result or error message.
Here is the sample code:
def insert_record(table_name, name, age): with MySQLdb.connect(**OCEANBASE_CONFIG) as conn: with conn.cursor() as cursor: try: insert_sql = f"INSERT INTO {table_name} (name, age) VALUES (%s, %s)" cursor.execute(insert_sql, (name, age)) conn.commit() print("Record inserted successfully") except MySQLdb.Error as err: print(err)- Define the
Define a function for querying table data.
- Define the
select_allfunction to query all records in a specified table. - Use the
withstatement to manage the lifecycle of database connections and cursor objects. This ensures that database connections and cursor objects can be securely closed without memory leaks. - Define an SQL statement, execute the statement, traverse query results, and print all records. If an error occurs, capture the error and print the error message.
Here is the sample code:
def select_all(table_name): with MySQLdb.connect(**OCEANBASE_CONFIG) as conn: with conn.cursor() as cursor: try: select_sql = f"SELECT * FROM {table_name}" cursor.execute(select_sql) result = cursor.fetchall() for row in result: print(row) except MySQLdb.Error as err: print(err)- Define the
Code in main.py
The main.py file in this topic shows how to use Python and MySQLdb to operate a database. The file provides a CLI where you can run commands to perform database operations. You can use this application to implement basic database operations such as creating tables, inserting records, and querying all records.
To configure the main.py file, perform the following steps:
Import the functions defined in the
db.pyfile.Import the
create_table,insert_record, andselect_allfunctions from thedbmodule.Here is the sample code:
from db import create_table, insert_record, select_allDefine a function for operating the database.
Define the
mainfunction to implement a simple CLI application for database operations. The application calls thecreate_tablefunction to create a table namedtest_tbl1, and then enters awhileloop to wait for you to enter a command. The application calls the corresponding function to insert or query data based on your command until you enter the3command to exit the application. If you enter an invalid command, the application prompts you to re-enter a command.Command
1is to insert data,2is to query data, and3is to exit the application. If you enter an invalid command, the application prompts you to re-enter a command.Here is the sample code:
def main(): create_table() while True: print("Instruction: 1.Insert Data; 2.Query Data; 3.Exit.") command = input("Enter the command [1/2/3]> ") if command == "1": name = input("Enter name:") age = input("Enter age:") insert_record("test_tbl1", name, age) elif command == "2": select_all("test_tbl1") elif command == "3": break else: print("Invalid command, please enter command again [1/2/3]")Set the application scenario of the
mainfunction.Set the
mainfunction to be called only when themain.pyfile is directly executed. If themainmodule is just imported into another module, themainfunction is not called.Here is the sample code:
if __name__ == "__main__": main()Note
This setting can avoid automatic execution of the
mainfunction when themainmodule is imported, thus ensuring the reusability and extensibility of the application.
Complete code examples
OCEANBASE_CONFIG = {
'host': 'localhost',
'port': port,
'user': 'user_name',
'password': '',
'database': 'db_name',
'charset': 'utf8mb4'
}
import MySQLdb
from config import OCEANBASE_CONFIG
def create_table():
with MySQLdb.connect(**OCEANBASE_CONFIG) as conn:
with conn.cursor() as cursor:
try:
create_table_sql = """
CREATE TABLE test_tbl1 (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
age INT UNSIGNED NOT NULL,
PRIMARY KEY (id)
) ENGINE=OCEANBASE AUTO_INCREMENT=1
"""
cursor.execute(create_table_sql)
print("Table created successfully")
except MySQLdb.Error as err:
print(err)
def insert_record(table_name, name, age):
with MySQLdb.connect(**OCEANBASE_CONFIG) as conn:
with conn.cursor() as cursor:
try:
insert_sql = f"INSERT INTO {table_name} (name, age) VALUES (%s, %s)"
cursor.execute(insert_sql, (name, age))
conn.commit()
print("Record inserted successfully")
except MySQLdb.Error as err:
print(err)
def select_all(table_name):
with MySQLdb.connect(**OCEANBASE_CONFIG) as conn:
with conn.cursor() as cursor:
try:
select_sql = f"SELECT * FROM {table_name}"
cursor.execute(select_sql)
result = cursor.fetchall()
for row in result:
print(row)
except MySQLdb.Error as err:
print(err)
from db import create_table, insert_record, select_all
def main():
create_table()
while True:
print("Instruction: 1.Insert Data; 2.Query Data; 3.Exit.")
command = input("Enter the command [1/2/3]> ")
if command == "1":
name = input("Enter name:")
age = input("Enter age:")
insert_record("test_tbl1", name, age)
elif command == "2":
select_all("test_tbl1")
elif command == "3":
break
else:
print("Invalid command, please enter command again [1/2/3]")
if __name__ == "__main__":
main()
References
For more information about how to connect to OceanBase Database, see Connection methods.
For more information about how to create a database, see CREATE DATABASE.
Download the python-mysqlclient sample project