How to use MySQL and Python to develop a simple e-commerce platform
Introduction:
With the rapid development of the Internet, e-commerce platforms have become more and more Popular way to shop. In order to help readers understand how to use MySQL and Python to develop a simple e-commerce platform, this article will provide specific code examples and introduce the key steps in the development process.
Step 1: Create database
First, we need to create a database to store the data of the e-commerce platform. Use the MySQL command line or graphical interface to create a new database named "ecommerce". Create three tables in this database, namely "users", "products" and "orders". The structure of each table is as follows:
users table:
products table:
orders table :
Step 2: Connect to the database
We will use Python’s MySQL Connector module to connect MySQL database. First, you need to install the module and import the necessary libraries.
import mysql.connector
cnx = mysql.connector.connect(
host="localhost", user="your_username", password="your_password", database="ecommerce"
)
Step 3: User Registration and login function
Users need to register and log in to the e-commerce platform before shopping. We can use Python to create two functions to implement user registration and login functions.
def register_user(username, password):
# 检查用户名是否已存在 cursor = cnx.cursor() query = "SELECT * FROM users WHERE username = %s" cursor.execute(query, (username,)) result = cursor.fetchone() if result: print("Username already exists.") return # 将用户信息插入到数据库 query = "INSERT INTO users (username, password) VALUES (%s, %s)" cursor.execute(query, (username, password)) cnx.commit() print("User registered successfully.")
def login_user(username, password):
# 检查用户名和密码是否匹配 cursor = cnx.cursor() query = "SELECT * FROM users WHERE username = %s AND password = %s" cursor.execute(query, (username, password)) result = cursor.fetchone() if result: print("Login successful.") else: print("Incorrect username or password.")
Step 4: Product management function
On the e-commerce platform , sellers need to add and manage product information. We can use Python to create the following functions to add, edit, and delete items.
def add_product(name, price, quantity):
# 将商品信息插入到数据库 cursor = cnx.cursor() query = "INSERT INTO products (name, price, quantity) VALUES (%s, %s, %s)" cursor.execute(query, (name, price, quantity)) cnx.commit() print("Product added successfully.")
def update_product(product_id, price, quantity):
# 更新商品信息 cursor = cnx.cursor() query = "UPDATE products SET price = %s, quantity = %s WHERE id = %s" cursor.execute(query, (price, quantity, product_id)) cnx.commit() print("Product updated successfully.")
def delete_product(product_id):
# 删除商品 cursor = cnx.cursor() query = "DELETE FROM products WHERE id = %s" cursor.execute(query, (product_id,)) cnx.commit() print("Product deleted successfully.")
Step 5: Order Management Function
Users can place orders on the e-commerce platform. The following code example is used to implement the order submission and cancellation functions.
def place_order(user_id, product_id, quantity):
# 获取商品单价 cursor = cnx.cursor() query = "SELECT price FROM products WHERE id = %s" cursor.execute(query, (product_id,)) result = cursor.fetchone() if result: price = result[0] total_amount = price * quantity # 创建订单并插入到数据库 query = "INSERT INTO orders (user_id, product_id, quantity, total_amount) VALUES (%s, %s, %s, %s)" cursor.execute(query, (user_id, product_id, quantity, total_amount)) cnx.commit() print("Order placed successfully.") else: print("Product not found.")
def cancel_order(order_id):
# 删除订单 cursor = cnx.cursor() query = "DELETE FROM orders WHERE id = %s" cursor.execute(query, (order_id,)) cnx.commit() print("Order canceled successfully.")
Summary:
This article provides the use of MySQL and Python to develop a Steps and code examples for a simple e-commerce platform. By using the MySQL Connector library in Python, we can connect and operate MySQL databases. With the help of these codes, we can implement user registration, login, and product and order management functions. In actual development, you can continuously expand and improve these functions according to your needs, thereby creating a more complete e-commerce platform.
The above is the detailed content of How to develop a simple e-commerce platform using MySQL and Python. For more information, please follow other related articles on the PHP Chinese website!