码迷,mamicode.com
首页 > 数据库 > 详细

tornado硬件监控系统-连接数据库与数据迁移(2)

时间:2020-06-14 00:59:24      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:name   main   div   col   engine   硬件   mysq   mic   loading   

#-*- 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

 查看结果:

技术图片

 

tornado硬件监控系统-连接数据库与数据迁移(2)

标签:name   main   div   col   engine   硬件   mysq   mic   loading   

原文地址:https://www.cnblogs.com/topass123/p/13098656.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!