Basic CRUD Demo
This example demonstrates basic CRUD (Create, Read, Update, Delete) operations with PyTiDB.
- Use PyTiDB Client to connect to TiDB
- Create a table with text, vector, and JSON columns
- Perform basic CRUD operations on data
Prerequisites
- Python 3.10+
- A TiDB Cloud Serverless cluster: Create a free cluster here: tidbcloud.com ↗️
How to run
Step 1: Clone the repository to local
Step 2: Install the required packages
Step 3: Set up environment to connect to database
Go to TiDB Cloud console and get the connection parameters, then set up the environment variable like this:
cat > .env <<EOF
TIDB_HOST={gateway-region}.prod.aws.tidbcloud.com
TIDB_PORT=4000
TIDB_USERNAME={prefix}.root
TIDB_PASSWORD={password}
TIDB_DATABASE=test
EOF
Step 4: Run the demo
Expected output:
=== CREATE TABLE ===
Table created
=== TRUNCATE TABLE ===
Table truncated
=== CREATE ===
Created 3 items
=== READ ===
ID: 1, Content: TiDB is a distributed SQL database, Metadata: {'category': 'database'}
ID: 2, Content: GPT-4 is a large language model, Metadata: {'category': 'llm'}
ID: 3, Content: LlamaIndex is a Python library for building AI-powered applications, Metadata: {'category': 'rag'}
=== UPDATE ===
Updated item #1
After update - ID: 1, Content: TiDB Cloud Serverless is a fully-managed, auto-scaling cloud database service, Metadata: {'category': 'dbass'}
=== DELETE ===
Deleted item #2
=== FINAL STATE ===
ID: 1, Content: TiDB Cloud Serverless is a fully-managed, auto-scaling cloud database service, Metadata: {'category': 'dbass'}
ID: 3, Content: LlamaIndex is a Python library for building AI-powered applications, Metadata: {'category': 'rag'}
=== COUNT ROWS ===
Number of rows: 2
=== DROP TABLE ===
Table dropped
Basic CRUD operations completed!
Related Resources
- Source Code: View on GitHub
-
Category: Getting-Started
-
Description: Learn fundamental PyTiDB operations including database connection, table creation, and data manipulation.