''' 定义表的结构,并在数据库中创建对应的数据表 ''' __author__ = 'lmstack' from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, String, create_engine, Integer, DateTime, BigInteger, FLOAT, TIMESTAMP, func, Text from urllib import parse Base = declarative_base() class ConsistencyDeltaSoc(Base): __tablename__ = "remainchargetime" __table_args__ = ({'comment': '充电剩余时间'}) # 添加索引和表注释 id = Column(Integer, primary_key=True, autoincrement=True, comment="主键") add_time = Column(TIMESTAMP(True), server_default=func.now(), comment='记录创建时间') # 创建时间 update_time = Column(TIMESTAMP(True), nullable=False, server_default=func.now(), onupdate=func.now(), comment='记录更新时间') # 更新时间 time = Column(TIMESTAMP(True), comment="时间") current = Column(FLOAT, comment="总电流") voltage = Column(FLOAT, comment="总电压") soc = Column(FLOAT, comment="soc") soh = Column(FLOAT, comment="soh") remain_time = Column(FLOAT, comment="剩余充电时间/s") remain_time_str = Column(String(64), comment="剩余充电时间/时分秒") max_volt = Column(FLOAT, comment="最高单体电压") max_temp = Column(FLOAT, comment="最高单体温度") min_temp = Column(FLOAT, comment="最低单体温度") sn = Column(String(64), comment="sn") # 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 # 执行该文件,创建表格到对应的数据库中 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)