12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- '''
- 定义表的结构,并在数据库中创建对应的数据表
- '''
- __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 DrivingRangeSnFactorNewest(Base):
- __tablename__ = "driving_range_sn_factor_newest"
- __table_args__ = ({'comment': '续驶里程 最新sn factor'}) # 添加索引和表注释
- # 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='记录更新时间') # 更新时间
- sn = Column(String(64), comment="sn,主键", primary_key=True)
- date = Column(DateTime, comment="日期")
- a0 = Column(FLOAT, comment="系数")
- a1 = Column(FLOAT, comment="系数")
- a2 = Column(FLOAT, comment="系数")
- a3 = Column(FLOAT, comment="系数")
- a4 = Column(FLOAT, comment="系数")
- def __init__(self, sn, date, a0, a1, a2, a3, a4):
- self.sn = sn
- self.date = date
- self.a0 = a0
- self.a1 = a1
- self.a2 = a2
- self.a3 = a3
- self.a4 = a4
- # 执行该文件,创建表格到对应的数据库中
- 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)
|