create_table.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. '''
  2. 定义表的结构,并在数据库中创建对应的数据表
  3. '''
  4. __author__ = 'Wang Liming'
  5. from sqlalchemy.ext.declarative import declarative_base
  6. from sqlalchemy import Column, String, create_engine, Integer, DateTime, BigInteger, FLOAT
  7. Base = declarative_base()
  8. class BmsLastDataDay(Base):
  9. __tablename__ = "bms_last_data_day"
  10. __table_args__ = ({'comment': '电池信号监控---每天最后一条bms数据'}) # 添加索引和表注释
  11. id = Column(Integer, primary_key=True, autoincrement=True, comment="主键")
  12. sn = Column(String(64), comment="sn")
  13. current = Column(FLOAT, comment="电流")
  14. time_stamp = Column(DateTime, comment="时间戳")
  15. pack_state = Column(Integer, comment="电池状态")
  16. line_state = Column(Integer, comment="信号状态")
  17. def __init__(self, sn, current, time_stamp, pack_state, line_state):
  18. self.sn = sn
  19. self.current = current
  20. self.time_stamp = time_stamp
  21. self.pack_state = pack_state
  22. self.line_state = line_state
  23. class GpsLastDataDay(Base):
  24. __tablename__ = "gps_last_data_day"
  25. __table_args__ = ({'comment': '电池信号监控---每天最后一条gps数据'}) # 添加索引和表注释
  26. id = Column(Integer, primary_key=True, autoincrement=True, comment="主键")
  27. sn = Column(String(64), comment="sn")
  28. time_stamp = Column(DateTime, comment="时间戳")
  29. pack_state = Column(Integer, comment="电池状态")
  30. line_state = Column(Integer, comment="信号状态")
  31. def __init__(self, sn, time_stamp, pack_state, line_state):
  32. self.sn = sn
  33. self.time_stamp = time_stamp
  34. self.pack_state = pack_state
  35. self.line_state = line_state
  36. class GpsSignalMonitor(Base):
  37. __tablename__ = "gps_signal_monitor"
  38. __table_args__ = ({'comment': 'gps信号监控'}) # 添加索引和表注释
  39. id = Column(Integer, primary_key=True, autoincrement=True, comment="主键")
  40. sn = Column(String(64), comment="sn")
  41. start_time = Column(DateTime, comment="开始时间")
  42. end_time = Column(DateTime, comment="结束时间")
  43. offline_time = Column(DateTime, comment="离线时间")
  44. pack_state = Column(Integer, comment="电池状态")
  45. line_state = Column(Integer, comment="信号状态")
  46. def __init__(self, sn, start_time, end_time, off_line_time, pack_state, line_state):
  47. self.sn = sn
  48. self.start_time = start_time
  49. self.end_time = end_time
  50. self.off_line_time = off_line_time
  51. self.pack_state = pack_state
  52. self.line_state = line_state
  53. class BmsSignalMonitor(Base):
  54. __tablename__ = "bms_signal_monitor"
  55. __table_args__ = ({'comment': 'bms信号监控'}) # 添加索引和表注释
  56. id = Column(Integer, primary_key=True, autoincrement=True, comment="主键")
  57. sn = Column(String(64), comment="sn")
  58. start_time = Column(DateTime, comment="开始时间")
  59. end_time = Column(DateTime, comment="结束时间")
  60. offline_time = Column(DateTime, comment="离线时间")
  61. pack_state = Column(Integer, comment="电池状态")
  62. line_state = Column(Integer, comment="信号状态")
  63. def __init__(self, sn, start_time, end_time, off_line_time, pack_state, line_state):
  64. self.sn = sn
  65. self.start_time = start_time
  66. self.end_time = end_time
  67. self.off_line_time = off_line_time
  68. self.pack_state = pack_state
  69. self.line_state = line_state
  70. # 执行该文件,创建表格到对应的数据库中
  71. if __name__ == "__main__":
  72. host = 'rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  73. port = 3306
  74. user = 'qx_cas'
  75. password = 'Qx@123456'
  76. database = 'qx_cas'
  77. db_engine = create_engine(
  78. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  79. user, password, host, port, database
  80. ))
  81. Base.metadata.create_all(db_engine)