设计一个图书订购应用程序供订户使用,具备的功能模块:我的信息、书目查询、我的订单三个模块。 功能具体要求: “我的信息”能够查询和修改订户相关信息,例如手机号、电子邮件、地址等信息,订户代号唯一且不能进行修改。“书目查询”能查询出书目信息,包括书号(ISBN)、书名、出版单位、单价、库存量,库存量大于1的可以订购生成订单。“我的订单”能够看到订单信息,包括订单号、订户代号、订购书目和数量等信息。 用Python和pymysql
时间: 2023-11-27 17:50:11 浏览: 158
实现该图书订购应用程序,需要先安装pymysql库,可以使用以下命令:
```python
pip install pymysql
```
接下来,可以按照以下步骤实现该应用程序:
1. 连接数据库
```python
import pymysql
# 连接数据库
db = pymysql.connect(host='localhost', port=3306, user='root', password='password', database='bookstore')
cursor = db.cursor()
```
2. 实现“我的信息”模块
```python
def get_user_info(user_id):
# 查询订户信息
sql = "SELECT * FROM `user` WHERE `user_id`=%s"
cursor.execute(sql, user_id)
result = cursor.fetchone()
if result:
return {
'user_id': result[0],
'username': result[1],
'phone': result[2],
'email': result[3],
'address': result[4]
}
else:
return None
def update_user_info(user_id, phone, email, address):
# 更新订户信息
sql = "UPDATE `user` SET `phone`=%s, `email`=%s, `address`=%s WHERE `user_id`=%s"
cursor.execute(sql, (phone, email, address, user_id))
db.commit()
```
3. 实现“书目查询”模块
```python
def search_books(keyword):
# 查询书目信息
sql = "SELECT * FROM `book` WHERE `name` LIKE %s AND `stock` > 0"
cursor.execute(sql, f'%{keyword}%')
results = cursor.fetchall()
books = []
for result in results:
books.append({
'book_id': result[0],
'isbn': result[1],
'name': result[2],
'publisher': result[3],
'price': result[4],
'stock': result[5]
})
return books
def create_order(user_id, book_id, qty):
# 创建订单
sql = "INSERT INTO `order` (`user_id`, `book_id`, `qty`) VALUES (%s, %s, %s)"
cursor.execute(sql, (user_id, book_id, qty))
db.commit()
# 减少库存
sql = "UPDATE `book` SET `stock`=`stock`-%s WHERE `book_id`=%s"
cursor.execute(sql, (qty, book_id))
db.commit()
```
4. 实现“我的订单”模块
```python
def get_user_orders(user_id):
# 查询订户订单
sql = "SELECT * FROM `order` WHERE `user_id`=%s"
cursor.execute(sql, user_id)
results = cursor.fetchall()
orders = []
for result in results:
book_id = result[2]
qty = result[3]
book_sql = "SELECT * FROM `book` WHERE `book_id`=%s"
cursor.execute(book_sql, book_id)
book_result = cursor.fetchone()
if book_result:
book = {
'book_id': book_result[0],
'isbn': book_result[1],
'name': book_result[2],
'publisher': book_result[3],
'price': book_result[4],
'stock': book_result[5]
}
orders.append({
'order_id': result[0],
'user_id': result[1],
'book': book,
'qty': qty
})
return orders
```
完整代码如下:
```python
import pymysql
# 连接数据库
db = pymysql.connect(host='localhost', port=3306, user='root', password='password', database='bookstore')
cursor = db.cursor()
def get_user_info(user_id):
# 查询订户信息
sql = "SELECT * FROM `user` WHERE `user_id`=%s"
cursor.execute(sql, user_id)
result = cursor.fetchone()
if result:
return {
'user_id': result[0],
'username': result[1],
'phone': result[2],
'email': result[3],
'address': result[4]
}
else:
return None
def update_user_info(user_id, phone, email, address):
# 更新订户信息
sql = "UPDATE `user` SET `phone`=%s, `email`=%s, `address`=%s WHERE `user_id`=%s"
cursor.execute(sql, (phone, email, address, user_id))
db.commit()
def search_books(keyword):
# 查询书目信息
sql = "SELECT * FROM `book` WHERE `name` LIKE %s AND `stock` > 0"
cursor.execute(sql, f'%{keyword}%')
results = cursor.fetchall()
books = []
for result in results:
books.append({
'book_id': result[0],
'isbn': result[1],
'name': result[2],
'publisher': result[3],
'price': result[4],
'stock': result[5]
})
return books
def create_order(user_id, book_id, qty):
# 创建订单
sql = "INSERT INTO `order` (`user_id`, `book_id`, `qty`) VALUES (%s, %s, %s)"
cursor.execute(sql, (user_id, book_id, qty))
db.commit()
# 减少库存
sql = "UPDATE `book` SET `stock`=`stock`-%s WHERE `book_id`=%s"
cursor.execute(sql, (qty, book_id))
db.commit()
def get_user_orders(user_id):
# 查询订户订单
sql = "SELECT * FROM `order` WHERE `user_id`=%s"
cursor.execute(sql, user_id)
results = cursor.fetchall()
orders = []
for result in results:
book_id = result[2]
qty = result[3]
book_sql = "SELECT * FROM `book` WHERE `book_id`=%s"
cursor.execute(book_sql, book_id)
book_result = cursor.fetchone()
if book_result:
book = {
'book_id': book_result[0],
'isbn': book_result[1],
'name': book_result[2],
'publisher': book_result[3],
'price': book_result[4],
'stock': book_result[5]
}
orders.append({
'order_id': result[0],
'user_id': result[1],
'book': book,
'qty': qty
})
return orders
# 测试代码
user_info = get_user_info(1)
print(user_info)
update_user_info(1, '123456789', 'test@example.com', 'test address')
books = search_books('Python')
print(books)
create_order(1, 1, 1)
orders = get_user_orders(1)
print(orders)
# 关闭数据库连接
db.close()
```
需要注意的是,该应用程序还需要在数据库中创建相应的表,可以使用以下SQL语句:
```sql
CREATE TABLE `user` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`phone` varchar(20) DEFAULT NULL,
`email` varchar(100) DEFAULT NULL,
`address` varchar(200) DEFAULT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `book` (
`book_id` int(11) NOT NULL AUTO_INCREMENT,
`isbn` varchar(20) NOT NULL,
`name` varchar(100) NOT NULL,
`publisher` varchar(100) NOT NULL,
`price` decimal(10,2) NOT NULL,
`stock` int(11) NOT NULL,
PRIMARY KEY (`book_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `order` (
`order_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`book_id` int(11) NOT NULL,
`qty` int(11) NOT NULL,
PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
以上SQL语句只是示例,实际应用程序可能需要根据需求进行相应的修改。
阅读全文