#-*- coding: utf-8 -*- from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.dialects.mysql import BIGINT,DECIMAL,DATE,TIME,DATETIME from sqlalchemy import Column #调用 Base = declarative_base() #连接数据创建元类做映射 metadata = Base.metadata ''' 导入模型的继承父类 导入数据类型 导入创建字段类 定义模型 ''' #内存统计 class Mem(Base): __tablename__ = "mem" id = Column(BIGINT,primary_key=True) percent = Column(DECIMAL(6,2)) total = Column(DECIMAL(8,2)) used = Column(DECIMAL(8,2)) free = Column(DECIMAL(8,2)) create_date = Column(DATE) create_time = Column(TIME) create_dt = Column(DATETIME) #交换分区 class Swap(Base): __tablename__ = "swap" id = Column(BIGINT,primary_key=True) percent = Column(DECIMAL(6,2)) total = Column(DECIMAL(8,2)) used = Column(DECIMAL(8,2)) free = Column(DECIMAL(8,2)) create_date = Column(DATE) create_time = Column(TIME) create_dt = Column(DATETIME) #cpu统计 class Cpu(Base): __tablename__ = "cpu" id = Column(BIGINT,primary_key=True) percent = Column(DECIMAL(6,2)) create_date = Column(DATE) create_time = Column(TIME) create_dt = Column(DATETIME) if __name__ == "__main__": import mysql.connector from sqlalchemy import create_engine mysql_configs = dict( db_host="127.0.0.1", db_name="moniter", db_port=3306, db_user="root", db_pwd="", ) ''' 连接的格式:mysql+驱动://用户:密码@主机:端口/数据库名称 ''' link = "mysql+mysqlconnector://{db_user}:{db_pwd}@{db_host}:{db_port}/{db_name}".format( **mysql_configs ) #创建连接,echo输出日志 engine = create_engine(link,encoding="utf-8",echo=True) #映射 metadata.create_all(engine)
登录数据库,创建上面的数据库名字
数据迁移,终端运行:
(venv) ******\moniter\app\models>
Python models.py
查看结果:
本文来自博客园,作者:topass123,转载请注明原文链接:https://www.cnblogs.com/topass123/p/13098656.html