''' 定义表的结构,并在数据库中创建对应的数据表 ''' __author__ = 'lmstack' from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, String, create_engine, Integer, DateTime, BigInteger, FLOAT, TIMESTAMP, func from urllib import parse Base = declarative_base() class BmsLastDataDay(Base): __tablename__ = "signal_monitor_bms_last_day" __table_args__ = ({'comment': '电池信号监控---每天最后一条bms数据'}) # 添加索引和表注释 id = Column(Integer, primary_key=True, autoincrement=True, comment="主键") add_time = Column(TIMESTAMP(True), server_default=func.now()) # 创建时间 update_time = Column(TIMESTAMP(True), nullable=False, server_default=func.now(), onupdate=func.now()) # 更新时间 sn = Column(String(64), comment="sn") current = Column(FLOAT, comment="电流") time_stamp = Column(DateTime, comment="时间戳") pack_state = Column(Integer, comment="电池状态") line_state = Column(Integer, comment="信号状态") # def __init__(self, sn, current, time_stamp, pack_state, line_state): # self.sn = sn # self.current = current # self.time_stamp = time_stamp # self.pack_state = pack_state # self.line_state = line_state class GpsLastDataDay(Base): __tablename__ = "signal_monitor_gps_last_day" __table_args__ = ({'comment': '电池信号监控---每天最后一条gps数据'}) # 添加索引和表注释 id = Column(Integer, primary_key=True, autoincrement=True, comment="主键") add_time = Column(TIMESTAMP(True), server_default=func.now()) # 创建时间 update_time = Column(TIMESTAMP(True), nullable=False, server_default=func.now(), onupdate=func.now()) # 更新时间 sn = Column(String(64), comment="sn") time_stamp = Column(DateTime, comment="时间戳") pack_state = Column(Integer, comment="电池状态") line_state = Column(Integer, comment="信号状态") latitude = Column(FLOAT, comment="纬度") longitude = Column(FLOAT, comment="经度") # def __init__(self, sn, time_stamp, pack_state, line_state, ): # self.sn = sn # self.time_stamp = time_stamp # self.pack_state = pack_state # self.line_state = line_state class GpsSignalMonitor(Base): __tablename__ = "signal_monitor_gps" __table_args__ = ({'comment': 'gps信号监控'}) # 添加索引和表注释 id = Column(Integer, primary_key=True, autoincrement=True, comment="主键") add_time = Column(TIMESTAMP(True), server_default=func.now()) # 创建时间 update_time = Column(TIMESTAMP(True), nullable=False, server_default=func.now(), onupdate=func.now()) # 更新时间 sn = Column(String(64), comment="sn") start_time = Column(DateTime, comment="开始时间") end_time = Column(DateTime, comment="结束时间") offline_time = Column(Integer, comment="离线时间") pack_state = Column(Integer, comment="电池状态") line_state = Column(Integer, comment="信号状态") latitude = Column(FLOAT, comment="纬度") longitude = Column(FLOAT, comment="经度") # def __init__(self, sn, start_time, end_time, off_line_time, pack_state, line_state): # self.sn = sn # self.start_time = start_time # self.end_time = end_time # self.off_line_time = off_line_time # self.pack_state = pack_state # self.line_state = line_state class BmsSignalMonitor(Base): __tablename__ = "signal_monitor_bms" __table_args__ = ({'comment': 'bms信号监控'}) # 添加索引和表注释 id = Column(Integer, primary_key=True, autoincrement=True, comment="主键") add_time = Column(TIMESTAMP(True), server_default=func.now()) # 创建时间 update_time = Column(TIMESTAMP(True), nullable=False, server_default=func.now(), onupdate=func.now()) # 更新时间 sn = Column(String(64), comment="sn") start_time = Column(DateTime, comment="开始时间") end_time = Column(DateTime, comment="结束时间") offline_time = Column(Integer, comment="离线时间") pack_state = Column(Integer, comment="电池状态") line_state = Column(Integer, comment="信号状态") # def __init__(self, sn, start_time, end_time, off_line_time, pack_state, line_state): # self.sn = sn # self.start_time = start_time # self.end_time = end_time # self.off_line_time = off_line_time # self.pack_state = pack_state # self.line_state = line_state # 执行该文件,创建表格到对应的数据库中 if __name__ == "__main__": host = 'rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com' port = 3306 user = 'qx_cas' password = parse.quote_plus('Qx@123456') database = 'qx_cas' db_engine = create_engine( "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format( user, password, host, port, database )) Base.metadata.create_all(db_engine)