create_table.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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, Text
  7. from urllib import parse
  8. Base = declarative_base()
  9. class ConsistencyDeltaSoc(Base):
  10. __tablename__ = "odo_dailyMileageEstimation"
  11. __table_args__ = ({'comment': '每日续驶里程'}) # 添加索引和表注释
  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. time = Column(TIMESTAMP(True), comment="时间")
  16. sn = Column(String(64), comment="sn")
  17. current = Column(FLOAT, comment="总电流")
  18. voltage = Column(FLOAT, comment="总电压")
  19. soc = Column(FLOAT, comment="soc")
  20. charging_status = Column(Integer, comment="数据状态")
  21. latitude = Column(FLOAT, comment="纬度")
  22. longitude = Column(FLOAT, comment="经度")
  23. energy_consump = Column(FLOAT, comment="能耗")
  24. voltage = Column(FLOAT, comment="总电压")
  25. mileage = Column(FLOAT, comment='每日续驶里程 /km')
  26. # def __init__(self, sn, current, time_stamp, pack_state, line_state):
  27. # self.sn = sn
  28. # self.current = current
  29. # self.time_stamp = time_stamp
  30. # self.pack_state = pack_state
  31. # self.line_state = line_state
  32. # 执行该文件,创建表格到对应的数据库中
  33. if __name__ == "__main__":
  34. host = 'rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  35. port = 3306
  36. user = 'qx_cas'
  37. password = parse.quote_plus('Qx@123456')
  38. database = 'qx_cas'
  39. db_engine = create_engine(
  40. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  41. user, password, host, port, database
  42. ))
  43. Base.metadata.create_all(db_engine)