Skip to main content

Connect to seekdb by using peewee

This topic describes how to connect to seekdb by using peewee and perform basic database operations, such as creating tables, inserting data, updating data, and querying data.

Prerequisites

  • You have installed Python 3.x and pip.
  • You have installed seekdb.

Procedure

  1. Obtain the connection string of seekdb.
  2. Install peewee and the MySQL client library.
  3. Create a test.py file and enter the database connection information.
  4. Run the test.py file.

Step 1: Obtain the connection string of seekdb

Contact the deployment personnel or administrator of seekdb to obtain the corresponding database connection string.

mysql -h$host -P$port -u$user_name -p$password -D$database_name

Parameter description:

  • $host: the IP address for connecting to seekdb. Replace this parameter with the actual IP address. You can also use the local IP address, 127.0.0.1.
  • $port: the port for connecting to seekdb. Replace this parameter with the actual port number. The default value is 2881. You can customize the port number when you deploy seekdb.
  • $database_name: the name of the database to be accessed.
  • $user_name: the username for connecting to seekdb. Format: username.
  • $password: the password for connecting to seekdb.

For more information about the connection string, see Connect to seekdb by using a MySQL client.

Here is an example:

mysql -hxxx.xxx.xxx.xxx -P2881 -uroot -p****** -Dtest

Step 2: Install peewee and the MySQL client library

peewee is a lightweight Python ORM (object-relational mapping) library that provides a simple and powerful API for simplifying database operations. peewee supports multiple database backends, including MySQL, PostgreSQL, and SQLite.

Open the command prompt or PowerShell terminal and run the following commands to install peewee and the MySQL client library.

pip install peewee pymysql

After the installation is complete, run the following command to verify whether the installation was successful:

pip list | grep peewee
info

peewee is a lightweight ORM library that provides a simple and powerful API for simplifying database operations. peewee supports multiple database backends, including MySQL, PostgreSQL, and SQLite.

Step 3: Create a test.py file and enter the database connection information

Create a test.py file and enter the database connection information based on the information obtained in Step 1: Obtain the connection string of seekdb.

  1. Create a file named test.py.

  2. Enter the following content in the test.py file and modify the database connection information as needed.

    Here is an example of the content of the test.py file:

    from peewee import *

    # Database connection information
    DB_USER = 'root'
    DB_PASSWORD = '******'
    DB_HOST = 'xxx.xxx.xxx.xxx'
    DB_PORT = 2881
    DB_NAME = 'test'

    # Create a database connection
    db = MySQLDatabase(
    DB_NAME,
    user=DB_USER,
    password=DB_PASSWORD,
    host=DB_HOST,
    port=DB_PORT
    )

    # Define a model
    class User(Model):
    name = CharField(max_length=50)
    age = IntegerField()

    class Meta:
    database = db
    table_name = 'users'

    def __str__(self):
    return f"{self.name} ({self.age})"

    # Connect to the database
    db.connect()

    # Create a table
    db.create_tables([User])

    # Insert data
    User.create(name='John', age=20)
    User.create(name='Lucy', age=25)
    User.create(name='Tom', age=30)

    # Update data
    user = User.get(User.name == 'Lucy')
    user.age = 26
    user.save()

    # Query data
    users = User.select()
    for user in users:
    print(user)

    # Close the database connection
    db.close()

Step 4: Run the test.py file

Open the command prompt or PowerShell terminal, go to the directory where the test.py file is stored, and run the test.py file to query data and output the results.

  1. Go to the directory where the test.py file is stored.

    Here is an example:

    cd D:\demo\demo
  2. Run the test.py file.

    Here is an example:

    python test.py

    The returned result is as follows:

    John (20)
    Lucy (26)
    Tom (30)

Error handling

When you use peewee to connect to seekdb, you may encounter various errors. The following table describes some common errors and their solutions:

  1. Connection error: If you cannot connect to the database, check whether the connection parameters are correct, including the host name, port, username, password, and database name.

  2. Permission error: If you encounter a permission-related error, make sure that the user has sufficient permissions to perform the required operations.

  3. SQL syntax error: If the SQL statement has a syntax error, check whether the syntax of the SQL statement is correct.

  4. Data type error: If the inserted data type does not match the table definition, make sure that the inserted data type is correct.

In peewee, you can use the try-except statement to capture and handle these errors to ensure that the program can handle errors gracefully instead of crashing. You can also use the logging module in Python to record error information for easier debugging and troubleshooting.

References