create_table.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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__ = "signal_monitor_bms_last_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__ = "signal_monitor_gps_last_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. latitude = Column(FLOAT, comment="纬度")
  37. longitude = Column(FLOAT, comment="经度")
  38. # def __init__(self, sn, time_stamp, pack_state, line_state, ):
  39. # self.sn = sn
  40. # self.time_stamp = time_stamp
  41. # self.pack_state = pack_state
  42. # self.line_state = line_state
  43. class GpsSignalMonitor(Base):
  44. __tablename__ = "signal_monitor_gps"
  45. __table_args__ = ({'comment': 'gps信号监控'}) # 添加索引和表注释
  46. id = Column(Integer, primary_key=True, autoincrement=True, comment="主键")
  47. add_time = Column(TIMESTAMP(True), server_default=func.now()) # 创建时间
  48. update_time = Column(TIMESTAMP(True), nullable=False, server_default=func.now(), onupdate=func.now()) # 更新时间
  49. sn = Column(String(64), comment="sn")
  50. start_time = Column(DateTime, comment="开始时间")
  51. end_time = Column(DateTime, comment="结束时间")
  52. offline_time = Column(Integer, comment="离线时间")
  53. pack_state = Column(Integer, comment="电池状态")
  54. line_state = Column(Integer, comment="信号状态")
  55. latitude = Column(FLOAT, comment="纬度")
  56. longitude = Column(FLOAT, comment="经度")
  57. # def __init__(self, sn, start_time, end_time, off_line_time, pack_state, line_state):
  58. # self.sn = sn
  59. # self.start_time = start_time
  60. # self.end_time = end_time
  61. # self.off_line_time = off_line_time
  62. # self.pack_state = pack_state
  63. # self.line_state = line_state
  64. class BmsSignalMonitor(Base):
  65. __tablename__ = "signal_monitor_bms"
  66. __table_args__ = ({'comment': 'bms信号监控'}) # 添加索引和表注释
  67. id = Column(Integer, primary_key=True, autoincrement=True, comment="主键")
  68. add_time = Column(TIMESTAMP(True), server_default=func.now()) # 创建时间
  69. update_time = Column(TIMESTAMP(True), nullable=False, server_default=func.now(), onupdate=func.now()) # 更新时间
  70. sn = Column(String(64), comment="sn")
  71. start_time = Column(DateTime, comment="开始时间")
  72. end_time = Column(DateTime, comment="结束时间")
  73. offline_time = Column(Integer, comment="离线时间")
  74. pack_state = Column(Integer, comment="电池状态")
  75. line_state = Column(Integer, comment="信号状态")
  76. # def __init__(self, sn, start_time, end_time, off_line_time, pack_state, line_state):
  77. # self.sn = sn
  78. # self.start_time = start_time
  79. # self.end_time = end_time
  80. # self.off_line_time = off_line_time
  81. # self.pack_state = pack_state
  82. # self.line_state = line_state
  83. # 执行该文件,创建表格到对应的数据库中
  84. if __name__ == "__main__":
  85. host = 'rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  86. port = 3306
  87. user = 'qx_cas'
  88. password = parse.quote_plus('Qx@123456')
  89. database = 'qx_cas'
  90. db_engine = create_engine(
  91. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  92. user, password, host, port, database
  93. ))
  94. Base.metadata.create_all(db_engine)