create_table2.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. '''
  2. 定义表的结构,并在数据库中创建对应的数据表
  3. '''
  4. __author__ = 'lmstack'
  5. from sqlalchemy.ext.declarative import declarative_base
  6. from sqlalchemy import Column, String, create_engine, Integer, DateTime, BigInteger, FLOAT, TIMESTAMP, func
  7. from urllib import parse
  8. Base = declarative_base()
  9. class DrivingRangeResult(Base):
  10. __tablename__ = "driving_range_result"
  11. __table_args__ = ({'comment': '每5分钟计算一次续驶里程结果'}) # 添加索引和表注释
  12. id = Column(Integer, primary_key=True, autoincrement=True, comment="主键")
  13. add_time = Column(TIMESTAMP(True), server_default=func.now(), comment='记录创建时间') # 创建时间
  14. update_time = Column(TIMESTAMP(True), nullable=False, server_default=func.now(), onupdate=func.now(), comment='记录更新时间') # 更新时间
  15. sn = Column(String(64), comment="sn")
  16. time = Column(DateTime, comment="时间")
  17. soc = Column(FLOAT, comment="soc")
  18. a0 = Column(FLOAT, comment="系数")
  19. a1 = Column(FLOAT, comment="系数")
  20. a2 = Column(FLOAT, comment="系数")
  21. a3 = Column(FLOAT, comment="系数")
  22. a4 = Column(FLOAT, comment="系数")
  23. vehelecrng = Column(FLOAT, comment="续驶里程")
  24. def __init__(self, sn, time, soc, a0, a1, a2, a3, a4, vehelecrng):
  25. self.sn = sn
  26. self.time = time
  27. self.soc = soc
  28. self.a0 = a0
  29. self.a1 = a1
  30. self.a2 = a2
  31. self.a3 = a3
  32. self.a4 = a4
  33. self.vehelecrng = vehelecrng
  34. # 执行该文件,创建表格到对应的数据库中
  35. if __name__ == "__main__":
  36. host = 'rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  37. port = 3306
  38. user = 'qx_cas'
  39. password = parse.quote_plus('Qx@123456')
  40. database = 'qx_cas'
  41. db_engine = create_engine(
  42. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  43. user, password, host, port, database
  44. ))
  45. Base.metadata.create_all(db_engine)