Home > Database > Mysql Tutorial > body text

How to develop a simple online ticket booking system using MySQL and Python

PHPz
Release: 2023-09-20 14:37:48
Original
665 people have browsed it

How to develop a simple online ticket booking system using MySQL and Python

How to use MySQL and Python to develop a simple online ticket booking system

With the rapid development of the Internet, more and more people are beginning to use online services. Shopping, booking and more. Online ticket booking system is one of the common application scenarios. This article will introduce how to use MySQL and Python to develop a simple online ticket booking system and provide specific code examples.

  1. Database design

First, we need to design a database to store ticket information, user information and order information. The following is a simple database design example:

Ticket table (ticket):

  • ticket_id (primary key)
  • name (ticket name)
  • price (ticket price)
  • quantity (ticket quantity)

User table (user):

  • user_id (primary key)
  • name (user name)
  • email (user email)
  • password (user password)

Order form (order):

  • order_id (primary key)
  • user_id (foreign key, associated user table)
  • ticket_id (foreign key, associated ticket table)
  • quantity (ticket quantity)
  • total_price (total price of order)
  • order_time (order time)
  1. Create database and table

Use MySQL command line or Tools such as MySQL Workbench create a database named "ticket_booking", and then create the above three tables in the database.

  1. Connect to the database

In the Python code, we need to use MySQL to connect to the database. First, we need to install the MySQL driver for Python, which can be installed using the pip command:

pip install mysql-connector-python
Copy after login

Then, connect to the MySQL database through the following code:

import mysql.connector

# 创建数据库连接
mydb = mysql.connector.connect(
  host="localhost",
  user="username",
  password="password",
  database="ticket_booking"
)

# 创建游标对象
cursor = mydb.cursor()
Copy after login

In the code, you need to " Replace "username" and "password" with your MySQL database username and password. This code will connect to the database named "ticket_booking".

  1. Implement user registration and login functions

In order for users to use the system, we need to implement user registration and login functions. The following is a simple sample code:

def register_user(name, email, password):
    # 检查邮箱是否已被注册
    cursor.execute("SELECT * FROM user WHERE email = %s", (email,))
    if cursor.fetchone() is not None:
        print("该邮箱已被注册!")
        return False

    # 将用户信息插入数据库
    cursor.execute("INSERT INTO user (name, email, password) VALUES (%s, %s, %s)", (name, email, password))
    mydb.commit()
    print("用户注册成功!")
    return True

def login_user(email, password):
    # 查询用户信息
    cursor.execute("SELECT user_id FROM user WHERE email = %s AND password = %s", (email, password))
    user = cursor.fetchone()
    if user is not None:
        print("用户登录成功!")
        return user[0]
    
    print("邮箱或密码错误!")
    return None
Copy after login

In the above code, the register_user function inserts user registration information into the database and returns True after successful registration; the login_user function Then verify whether the user's email and password are correct, and return the user ID.

  1. Implement the ticket booking function

Now we will implement the online ticket booking function. The following is a simple implementation example:

def book_ticket(user_id, ticket_id, quantity):
    # 检查门票数量是否足够
    cursor.execute("SELECT quantity FROM ticket WHERE ticket_id = %s", (ticket_id,))
    ticket_quantity = cursor.fetchone()[0]
    if ticket_quantity < quantity:
        print("门票数量不足,请重新选择!")
        return False

    # 计算订单总价
    cursor.execute("SELECT price FROM ticket WHERE ticket_id = %s", (ticket_id,))
    ticket_price = cursor.fetchone()[0]
    total_price = ticket_price * quantity

    # 创建订单
    cursor.execute("INSERT INTO `order` (user_id, ticket_id, quantity, total_price) VALUES (%s, %s, %s, %s)",
                   (user_id, ticket_id, quantity, total_price))
    mydb.commit()
    print("订单创建成功!")
    return True
Copy after login

In the above code, the book_ticket function first checks whether the number of tickets is sufficient, then calculates the total order price and inserts the order information into the database.

  1. Test code

You can use the following code to test the function just implemented:

# 注册用户
register_user("张三", "zhangsan@gmail.com", "123456")

# 用户登录
user_id = login_user("zhangsan@gmail.com", "123456")

# 预订门票
book_ticket(user_id, 1, 3)
Copy after login

In the above example code, we first register a file named " Zhang San" user, then use the user's email and password to log in, and finally reserve 3 tickets with ID 1.

The above is an introduction and code example on how to use MySQL and Python to develop a simple online ticket booking system. You can modify and extend the code appropriately according to specific needs and scenarios to achieve more complete functions.

The above is the detailed content of How to develop a simple online ticket booking system using MySQL and Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!