create_table.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 BmsLastDataDay(Base):
  10. __tablename__ = "bms_last_data_day"
  11. __table_args__ = ({'comment': '电池信号监控---每天最后一条bms数据'}) # 添加索引和表注释
  12. id = Column(Integer, primary_key=True, autoincrement=True, comment="主键")
  13. add_time = Column(TIMESTAMP(True), server_default=func.now()) # 创建时间
  14. update_time = Column(TIMESTAMP(True), nullable=False, server_default=func.now(), onupdate=func.now()) # 更新时间
  15. sn = Column(String(64), comment="sn")
  16. current = Column(FLOAT, comment="电流")
  17. time_stamp = Column(DateTime, comment="时间戳")
  18. pack_state = Column(Integer, comment="电池状态")
  19. line_state = Column(Integer, comment="信号状态")
  20. def __init__(self, sn, current, time_stamp, pack_state, line_state):
  21. self.sn = sn
  22. self.current = current
  23. self.time_stamp = time_stamp
  24. self.pack_state = pack_state
  25. self.line_state = line_state
  26. class GpsLastDataDay(Base):
  27. __tablename__ = "gps_last_data_day"
  28. __table_args__ = ({'comment': '电池信号监控---每天最后一条gps数据'}) # 添加索引和表注释
  29. id = Column(Integer, primary_key=True, autoincrement=True, comment="主键")
  30. add_time = Column(TIMESTAMP(True), server_default=func.now()) # 创建时间
  31. update_time = Column(TIMESTAMP(True), nullable=False, server_default=func.now(), onupdate=func.now()) # 更新时间
  32. sn = Column(String(64), comment="sn")
  33. time_stamp = Column(DateTime, comment="时间戳")
  34. pack_state = Column(Integer, comment="电池状态")
  35. line_state = Column(Integer, comment="信号状态")
  36. def __init__(self, sn, time_stamp, pack_state, line_state):
  37. self.sn = sn
  38. self.time_stamp = time_stamp
  39. self.pack_state = pack_state
  40. self.line_state = line_state
  41. class GpsSignalMonitor(Base):
  42. __tablename__ = "gps_signal_monitor"
  43. __table_args__ = ({'comment': 'gps信号监控'}) # 添加索引和表注释
  44. id = Column(Integer, primary_key=True, autoincrement=True, comment="主键")
  45. add_time = Column(TIMESTAMP(True), server_default=func.now()) # 创建时间
  46. update_time = Column(TIMESTAMP(True), nullable=False, server_default=func.now(), onupdate=func.now()) # 更新时间
  47. sn = Column(String(64), comment="sn")
  48. start_time = Column(DateTime, comment="开始时间")
  49. end_time = Column(DateTime, comment="结束时间")
  50. offline_time = Column(Integer, comment="离线时间")
  51. pack_state = Column(Integer, comment="电池状态")
  52. line_state = Column(Integer, comment="信号状态")
  53. def __init__(self, sn, start_time, end_time, off_line_time, pack_state, line_state):
  54. self.sn = sn
  55. self.start_time = start_time
  56. self.end_time = end_time
  57. self.off_line_time = off_line_time
  58. self.pack_state = pack_state
  59. self.line_state = line_state
  60. class BmsSignalMonitor(Base):
  61. __tablename__ = "bms_signal_monitor"
  62. __table_args__ = ({'comment': 'bms信号监控'}) # 添加索引和表注释
  63. id = Column(Integer, primary_key=True, autoincrement=True, comment="主键")
  64. add_time = Column(TIMESTAMP(True), server_default=func.now()) # 创建时间
  65. update_time = Column(TIMESTAMP(True), nullable=False, server_default=func.now(), onupdate=func.now()) # 更新时间
  66. sn = Column(String(64), comment="sn")
  67. start_time = Column(DateTime, comment="开始时间")
  68. end_time = Column(DateTime, comment="结束时间")
  69. offline_time = Column(Integer, comment="离线时间")
  70. pack_state = Column(Integer, comment="电池状态")
  71. line_state = Column(Integer, comment="信号状态")
  72. def __init__(self, sn, start_time, end_time, off_line_time, pack_state, line_state):
  73. self.sn = sn
  74. self.start_time = start_time
  75. self.end_time = end_time
  76. self.off_line_time = off_line_time
  77. self.pack_state = pack_state
  78. self.line_state = line_state
  79. # 执行该文件,创建表格到对应的数据库中
  80. if __name__ == "__main__":
  81. host = 'rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  82. port = 3306
  83. user = 'qx_cas'
  84. password = parse.quote_plus('Qx@123456')
  85. database = 'qx_cas'
  86. db_engine = create_engine(
  87. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  88. user, password, host, port, database
  89. ))
  90. Base.metadata.create_all(db_engine)