tornado硬件监控系统-连接数据库与数据迁移(2)
#-*- 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
查看结果:
相关推荐
selectY 2020-07-18
zhangxuelong 2020-06-14
牧码人 2020-06-14
hjhmpl 2020-06-14
thundor 2020-05-05
Cagey 2020-04-25
KarlDoenitz 2020-04-16
牧码人 2020-01-25
KarlDoenitz 2019-12-28
hjhmpl 2019-12-25
hjhmpl 2019-12-17
selectY 2019-12-11
KarlDoenitz 2019-12-06
selectY 2019-12-05
Cagey 2019-12-05
hjhmpl 2019-11-03
牧码人 2019-11-03
chenzhanhai 2019-04-09
chenzhanhai 2019-09-09