create_table.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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__ = "mechanism_volsorout"
  11. __table_args__ = ({'comment': '电压内阻偏离检测'}) # 添加索引和表注释
  12. id = Column(Integer, primary_key=True, autoincrement=True, comment="主键")
  13. add_time = Column(TIMESTAMP(True), 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(TIMESTAMP(True), comment="计算时间")
  17. volsorout_confr = Column(Text, comment="电压内阻偏离状态(1析锂,0未析锂)")
  18. volsorout_amplt = Column(Text, comment="电压内阻偏离量")
  19. # def __init__(self, sn, current, time_stamp, pack_state, line_state):
  20. # self.sn = sn
  21. # self.current = current
  22. # self.time_stamp = time_stamp
  23. # self.pack_state = pack_state
  24. # self.line_state = line_state
  25. # 执行该文件,创建表格到对应的数据库中
  26. if __name__ == "__main__":
  27. host = 'rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  28. port = 3306
  29. user = 'qx_cas'
  30. password = parse.quote_plus('Qx@123456')
  31. database = 'qx_cas'
  32. db_engine = create_engine(
  33. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  34. user, password, host, port, database
  35. ))
  36. Base.metadata.create_all(db_engine)