• 主页

  • 投资

  • IT

    🔥
  • 设计

  • 销售

  • 共46篇

    python3.X - Web - Flask1.1.1

关闭

返回栏目

关闭

返回python3.X - Web - Flask1.1.1栏目

19 - Flask - 用代码创建数据库

作者:

贺及楼

成为作者

更新日期:2021-11-07 11:20:50

Flask - 用代码创建数据库
  1. from flask import Flask
  2. from flask_sqlalchemy import SQLAlchemy
  3. from _datetime import datetime
  4. app=Flask(__name__)
  5. app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://root:@127.0.0.1:3306/XXXXXX数据库名XXXXXX?charset=utf8mb4"
  6. app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
  7. app.config['SQLALCHEMY_ECHO'] = True
  8. db = SQLAlchemy(app)
  9. #数据库-Wechatdatabase表-用户
  10. class Wechatdatabase(db.Model):
  11. __tablename__ = 'wechat_user001'
  12. id = db.Column(db.Integer,primary_key=True)
  13. openid = db.Column(db.VARCHAR(255))
  14. nickname = db.Column(db.VARCHAR(255))
  15. sex = db.Column(db.Integer)
  16. province = db.Column(db.Text)
  17. city = db.Column(db.Text)
  18. country = db.Column(db.Text)
  19. unionid = db.Column(db.VARCHAR(255))#微信uid
  20. points = db.Column(db.Integer)#现在拥有的点数
  21. success_points = db.Column(db.Integer)#成功充值点数
  22. success_times = db.Column(db.Integer)#成功充值次数
  23. success_total_fee = db.Column(db.Integer)#成功充值金额
  24. orders = db.relationship('Wechatdatabaseorder',backref='nickname2')#这个用户的订单集合
  25. def __repr__(self):
  26. return 'Wechatdatabase:OK'
  27. #数据库-Wechatdatabaseorder表-订单
  28. class Wechatdatabaseorder(db.Model):
  29. __tablename__ = 'wechat_user_orders001'
  30. id = db.Column(db.Integer,primary_key=True)#订单id
  31. openid = db.Column(db.VARCHAR(255))
  32. nickname = db.Column(db.VARCHAR(255))
  33. unionid = db.Column(db.VARCHAR(255))
  34. out_trade_no = db.Column(db.VARCHAR(255))#订单号
  35. order_name = db.Column(db.VARCHAR(255))#订单名
  36. state = db.Column(db.VARCHAR(255))#订单状态未付款,已付款
  37. creat_time = db.Column(db.DateTime, default=datetime.now)#创建时间
  38. time_end = db.Column(db.VARCHAR(14))
  39. update_time = db.Column(db.DateTime, default=datetime.now,onupdate=datetime.now)#更新时间
  40. total_fee = db.Column(db.Integer)#订单价格
  41. spbill_create_ip = db.Column(db.VARCHAR(64))
  42. Wechatdatabase_id = db.Column(db.Integer,db.ForeignKey('wechat_user001.id'))#这个系统的下单的人id
  43. def __repr__(self):
  44. return 'Wechatdatabaseorder: ok'
  45. #db.drop_all() #谨慎开
  46. db.create_all() # 创建当前应用中声明的所有模型类对应的数据表,db.drop_all()是删除表
  47. @app.route("/")
  48. def index():
  49. return "hello"
  50. if __name__ == '__main__':
  51. app.run(debug=True)