当代数据库管理系统课程实验二
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

6.8 KiB

华东师范大学数据科学与工程学院实验报告

课程名称:当代数据库管理系统 年级 :2020级 上机实践成绩
指导教师 :高明 姓名 :杨舜、姚嘉和 学号 :10205501415、10205501436
上机实践名称 :BookStore 上机实践日期:2022.11.28 —— 2022.12.10
上机实践编号 组号 :21 上机实践时间:2022.11.28 —— 2022.12.10

实验过程

一. 分析原有的数据库结构

分析demo中/be/model/store.py中创建数据库表的sql语句可知原有数据库的结构的ER图大致如下 avatar

有上述ER图可以得到原有数据库表如下

user表:

user_id password balance token terminal
主键为user_id

store表:

store_id stock_level
主键为store_id

store_book表:

store_id book_id book_info stock_level
主键为联合主键(store_id,book_id)

user_store表:

user_id store_id
外键为(user_id,store_id)

new_order表:

order_id user_id store_id
主键为(order_id)

new_order_detail表:

oeder_id book_id count price
主键为联合主键(order_id,book_id)

 

二. 依据上述分析构建数据库table

  1. 利用sqlalchemy连接远程的aliyun数据库并创建上述table postgreSQLORM.py
class con:
 def connect():
     '''Returns a connection and a metadata object'''
     # We connect with the help of the PostgreSQL URL
     
     url = 'postgresql://stu10205501415:Stu10205501415@dase-cdms-2022-pub.pg.rds.aliyuncs.com:5432/stu10205501415'

     # The return value of create_engine() is our connection object
     con = create_engine(url, client_encoding='utf8')

     # We then bind the connection to MetaData()
     meta = MetaData(bind=con)

     return con, meta

class User(Base):
  __tablename__ = 'user'
 user_id = Column(TEXT, primary_key=True, comment="主键")
 password = Column(TEXT, nullable=False, comment="密码")
 balance = Column(Integer, nullable=False, comment="")
 token = Column(TEXT, comment="缓存的令牌")
 terminal = Column(TEXT, comment="终端代码")

class Store(Base):
 __tablename__ = 'store'
 store_id = Column(TEXT, primary_key=True,comment="主键")
 stock_level = Column(Integer, comment = "货存")

class Store_Book(Base):
 __tablename__ = 'store_book'
 store_id = Column(TEXT, comment="主键")
 book_id = Column(TEXT, comment="主键")
 book_info = Column(TEXT, comment="书籍信息")
 stock_level = Column(Integer, comment = "货存")

 __table_args__ = (
     PrimaryKeyConstraint('store_id', 'book_id'),
 )

class User_Store(Base):
 __tablename__ = 'user_store'

 id = Column(Integer, primary_key=True, autoincrement=True, comment="主键")

 fk_user_id = Column(
     TEXT,
     ForeignKey(
         "user.user_id",
         ondelete="CASCADE",
         onupdate="CASCADE",
     ),
     nullable=False,
     comment="user外键"
 )
 fk_store_id = Column(
     TEXT,
     ForeignKey(
         "store.store_id",
         ondelete="CASCADE",
         onupdate="CASCADE",
     ),
     nullable=False,
     comment="store外键"
 )
 # 多对多关系的中间表必须使用联合唯一约束,防止出现重复数据
 __table_args__ = (
     UniqueConstraint("fk_user_id", "fk_store_id"),
 )

class New_Order(Base):
 __tablename__ = 'new_order'
 order_id = Column(TEXT, primary_key = True, comment = '订单id')
 
 fk_user_id = Column(
     TEXT,
     ForeignKey(
         "user.user_id",
         ondelete="CASCADE",
         onupdate="CASCADE",
     ),
     nullable=False,
     comment="user外键"
 )
 fk_store_id = Column(
     TEXT,
     ForeignKey(
         "store.store_id",
         ondelete="CASCADE",
         onupdate="CASCADE",
     ),
     nullable=False,
     comment="store外键"
 )

class New_Order_Detail(Base):
 __tablename__ = 'new_order_detail'
 order_id = Column(TEXT, comment='订单id')
 book_id = Column(TEXT, comment='订单书籍')
 count = Column(Integer, comment='购买书籍数')
 price = Column(Integer, comment='单价')

 __table_args__ = (
     PrimaryKeyConstraint('order_id','book_id'),
 )
 
engine, meta = con.connect()
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
  1. 在上述创建的table中添加初始数据并利用该数据测试后端服务器与数据库的连接 avatar avatar

  2. 类比原有demo分别为不同的路由绑定不同的蓝图

 app.register_blueprint(auth.bp_auth)
 app.register_blueprint(seller.bp_seller)
 app.register_blueprint(buyer.bp_buyer)
  1. 在/be/model目录下创建User类用书实现User对于数据库的一些交互功能
  2. 修改model/db_conn.py中查询的操作为orm操作
from model import postgreSQLORM
from model.postgreSQLORM import User, Store_Book, User_Store

class DBConn:
 def __init__(self):
     return
     # self.conn = store.get_db_conn()

 def user_id_exist(self, user_id):
     row = postgreSQLORM.session.query(User).filter(User.user_id==user_id).one()
     # cursor = self.conn.execute("SELECT user_id FROM user WHERE user_id = ?;", (user_id,))
     # row = cursor.fetchone()
     if row is None:
         return False
     else:
         return True

 def book_id_exist(self, store_id, book_id):
     row = postgreSQLORM.session.query(Store_Book).filter(Store_Book.book_id==book_id and Store_Book.store_id==store_id).one()
     # cursor = self.conn.execute("SELECT book_id FROM store WHERE store_id = ? AND book_id = ?;", (store_id, book_id))
     # row = cursor.fetchone()
     if row is None:
         return False
     else:
         return True

 def store_id_exist(self, store_id):
     row = postgreSQLORM.session.query(User_Store).filter(User_Store.fk_store_id==store_id).one()
     # cursor = self.conn.execute("SELECT store_id FROM user_store WHERE store_id = ?;", (store_id,))
     # row = cursor.fetchone()
     if row is None:
         return False
     else:
         return True