create_table.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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__ = "cellStateEstimation_uniform_socVoltDiff"
  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. cellsoc_diff = Column(FLOAT, comment="soc差")
  18. cellvolt_diff = Column(FLOAT, comment="电压差")
  19. cellmin_num = Column(Integer, comment="最小电压编号")
  20. cellmax_num = Column(FLOAT, comment="最大电压编号")
  21. # def __init__(self, sn, current, time_stamp, pack_state, line_state):
  22. # self.sn = sn
  23. # self.current = current
  24. # self.time_stamp = time_stamp
  25. # self.pack_state = pack_state
  26. # self.line_state = line_state
  27. # 执行该文件,创建表格到对应的数据库中
  28. if __name__ == "__main__":
  29. host = 'rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  30. port = 3306
  31. user = 'qx_cas'
  32. password = parse.quote_plus('Qx@123456')
  33. database = 'qx_cas'
  34. db_engine = create_engine(
  35. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  36. user, password, host, port, database
  37. ))
  38. Base.metadata.create_all(db_engine)