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