OpenStack 資料庫操作 demo
1 #!/usr/bin/env python 2 3 from sqlalchemy.orm import sessionmaker 4 from sqlalchemy import create_engine 5 from sqlalchemy.ext.declarative import declarative_base 6 from sqlalchemy import Column, Integer, String, Float, Boolean 7 8 sql_connection = "mysql+pymysql://nova:[email protected]:3306/nova" 9 engine = create_engine(sql_connection, echo=True) 10 Session = sessionmaker(bind=engine) 11 session = Session() 12 13 BASE = declarative_base() 14 15 class InstanceTypes(BASE): 16 __tablename__ = "instance_types" 17 18 id = Column(Integer, primary_key=True) 19 name = Column(String(255))20 memory_mb = Column(Integer) 21 vcpus = Column(Integer) 22 root_gb = Column(Integer) 23 ephemeral_gb = Column(Integer) 24 flavorid = Column(String(255)) 25 swap = Column(Integer, nullable=False, default=0) 26 rxtx_factor = Column(Float, nullable=False, default=1) 27 vcpu_weight = Column(Integer, nullable=True)28 disabled = Column(Boolean, default=False) 29 is_public = Column(Boolean, default=True) 30 31 flavors = session.query(InstanceTypes).all() 32 33 for flavor in flavors: 34 print flavor.id, flavor.name
執行結果:
[[email protected] ~]# python sql.py
2018-11-01 22:35:08,943 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode'
2018-11-01 22:35:08,943 INFO sqlalchemy.engine.base.Engine ()
2018-11-01 22:35:08,944 INFO sqlalchemy.engine.base.Engine SELECT DATABASE()
2018-11-01 22:35:08,944 INFO sqlalchemy.engine.base.Engine ()
2018-11-01 22:35:08,944 INFO sqlalchemy.engine.base.Engine show collation where `Charset` = 'utf8' and `Collation` = 'utf8_bin'
2018-11-01 22:35:08,944 INFO sqlalchemy.engine.base.Engine ()
2018-11-01 22:35:08,946 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS CHAR(60)) AS anon_1
2018-11-01 22:35:08,946 INFO sqlalchemy.engine.base.Engine ()
2018-11-01 22:35:08,946 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS CHAR(60)) AS anon_1
2018-11-01 22:35:08,947 INFO sqlalchemy.engine.base.Engine ()
2018-11-01 22:35:08,947 INFO sqlalchemy.engine.base.Engine SELECT CAST('test collated returns' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin AS anon_1
2018-11-01 22:35:08,947 INFO sqlalchemy.engine.base.Engine ()
2018-11-01 22:35:08,948 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2018-11-01 22:35:08,949 INFO sqlalchemy.engine.base.Engine SELECT instance_types.id AS instance_types_id, instance_types.name AS instance_types_name, instance_types.memory_mb AS instance_types_memory_mb, instance_types.vcpus AS instance_types_vcpus, instance_types.root_gb AS instance_types_root_gb, instance_types.ephemeral_gb AS instance_types_ephemeral_gb, instance_types.flavorid AS instance_types_flavorid, instance_types.swap AS instance_types_swap, instance_types.rxtx_factor AS instance_types_rxtx_factor, instance_types.vcpu_weight AS instance_types_vcpu_weight, instance_types.disabled AS instance_types_disabled, instance_types.is_public AS instance_types_is_public
FROM instance_types
2018-11-01 22:35:08,949 INFO sqlalchemy.engine.base.Engine ()
1 m1.medium
2 m1.tiny
3 m1.large
4 m1.xlarge
5 m1.small
6 4Vcpu_4096Mem_50Disk_disk0type
7 m1.vcomputer