python自动化之路-day13

对上一节的补充:

1.ORM框架SQLAlchemy

SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,简言之便是:将对象转换成SQL,然后使用数据API执行SQL并获取执行结果。

python自动化之路-day13_第1张图片

Dialect用于和数据API进行交流,根据配置文件的不同调用不同的数据库API,从而实现对数据库的操作,如:

1
2
3
4
5
6
7
8
9
10
11
12
13
MySQL-Python
    mysql+mysqldb://:@[:]/
 
pymysql
    mysql+pymysql://:@/[?]
 
MySQL-Connector
    mysql+mysqlconnector://:@[:]/
 
cx_Oracle
    oracle+cx_oracle://user:pass@host:port/dbname[?key=value&key=value...]
 
更多详见:http://docs.sqlalchemy.org/en/latest/dialects/index.html

步骤一:

使用 Engine/ConnectionPooling/Dialect 进行数据库操作,Engine使用ConnectionPooling连接数据库,然后再通过Dialect执行SQL语句。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env python
# -*- coding:utf-8 -*-
 
from sqlalchemy import create_engine
 
 
engine = create_engine("mysql+mysqldb://root:[email protected]:3306/s11", max_overflow=5)
 
engine.execute(
    "INSERT INTO ts_test (a, b) VALUES ('2', 'v1')"
)
 
engine.execute(
     "INSERT INTO ts_test (a, b) VALUES (%s, %s)",
    ((555"v1"),(666"v1"),)
)
engine.execute(
    "INSERT INTO ts_test (a, b) VALUES (%(id)s, %(name)s)",
    id=999, name="v1"
)
 
result = engine.execute('select * from ts_test')
result.fetchall()
复制代码
#!/usr/bin/env python
# -*- coding:utf-8 -*-

from sqlalchemy import create_engine


engine = create_engine("mysql+mysqldb://root:[email protected]:3306/s11", max_overflow=5)


# 事务操作
with engine.begin() as conn:
    conn.execute("insert into table (x, y, z) values (1, 2, 3)")
    conn.execute("my_special_procedure(5)")
    
    
conn = engine.connect()
# 事务操作 
with conn.begin():
       conn.execute("some statement", {
       'x':5, 'y':10})
复制代码

注:查看数据库连接:show status like 'Threads%';

步骤二:

使用 Schema Type/SQL Expression Language/Engine/ConnectionPooling/Dialect 进行数据库操作。Engine使用Schema Type创建一个特定的结构对象,之后通过SQL Expression Language将该对象转换成SQL语句,然后通过 ConnectionPooling 连接数据库,再然后通过 Dialect 执行SQL,并获取结果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env python
# -*- coding:utf-8 -*-
 
from sqlalchemy import create_engine, Table, Column, Integer, String, MetaData, ForeignKey
 
metadata = MetaData()
 
user = Table('user', metadata,
    Column('id', Integer, primary_key=True),
    Column('name', String(20)),
)
 
color = Table('color', metadata,
    Column('id', Integer, primary_key=True),
    Column('name', String(20)),
)
engine = create_engine("mysql+mysqldb://root:[email protected]:3306/s11", max_overflow=5)
 
metadata.create_all(engine)
# metadata.clear()
# metadata.remove()
#!/usr/bin/env python
# -*- coding:utf-8 -*-

from sqlalchemy import create_engine, Table, Column, Integer, String, MetaData, ForeignKey

metadata = MetaData()

user = Table('user', metadata,
    Column('id', Integer, primary_key=True),
    Column('name', String(20)),
)

color = Table('color', metadata,
    Column('id', Integer, primary_key=True),
    Column('name', String(20)),
)
engine = create_engine("mysql+mysqldb://root:[email protected]:3306/s11", max_overflow=5)

conn = engine.connect()

# 创建SQL语句,INSERT INTO "user" (id, name) VALUES (:id, :name)
conn.execute(user.insert(),{
       'id':7,'name':'seven'})
conn.close()

# sql = user.insert().values(id=123, name='wu')
# conn.execute(sql)
# conn.close()

# sql = user.delete().where(user.c.id > 1)

# sql = user.update().values(fullname=user.c.name)
# sql = user.update().where(user.c.name == 'jack').values(name='ed')

# sql = select([user, ])
# sql = select([user.c.id, ])
# sql = select([user.c.name, color.c.name]).where(user.c.id==color.c.id)
# sql = select([user.c.name]).order_by(user.c.name)
# sql = select([user]).group_by(user.c.name)

# result = conn.execute(sql)
# print result.fetchall()
# conn.close()

更多内容详见:

    http://www.jianshu.com/p/e6bba189fcbd

    http://docs.sqlalchemy.org/en/latest/core/expression_api.html

注:SQLAlchemy无法修改表结构,如果需要可以使用SQLAlchemy开发者开源的另外一个软件Alembic来完成。

alembic学习笔记

步骤三:

使用 ORM/Schema Type/SQL Expression Language/Engine/ConnectionPooling/Dialect 所有组件对数据进行操作。根据类创建对象,对象转换成SQL,执行SQL。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
# -*- coding:utf-8 -*-
 
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
 
engine = create_engine("mysql+mysqldb://root:[email protected]:3306/s11", max_overflow=5)
 
Base = declarative_base()
 
 
class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    name = Column(String(50))
 
# 寻找Base的所有子类,按照子类的结构在数据库中生成对应的数据表信息
# Base.metadata.create_all(engine)
 
Session = sessionmaker(bind=engine)
session = Session()
 
 
# ########## 增 ##########
# u = User(id=2, name='sb')
# session.add(u)
# session.add_all([
#     User(id=3, name='sb'),
#     User(id=4, name='sb')
# ])
# session.commit()
 
# ########## 删除 ##########
# session.query(User).filter(User.id > 2).delete()
# session.commit()
 
# ########## 修改 ##########
# session.query(User).filter(User.id > 2).update({'cluster_id' : 0})
# session.commit()
# ########## 查 ##########
# ret = session.query(User).filter_by(name='sb').first()
 
# ret = session.query(User).filter_by(name='sb').all()
# print ret
 
# ret = session.query(User).filter(User.name.in_(['sb','bb'])).all()
# print ret
 
# ret = session.query(User.name.label('name_label')).all()
# print ret,type(ret)
 
# ret = session.query(User).order_by(User.id).all()
# print ret
 
# ret = session.query(User).order_by(User.id)[1:3]
# print ret
# session.commit()

例子:
# _*_ coding:utf-8 _*_
import pymysql
from sqlalchemy import create_engine,Table,Column,MetaData,INTEGER,String,ForeignKey

metadata = MetaData()
user = Table("user",metadata,Column("id",INTEGER,primary_key=True),Column("name",String(20)))
color = Table("color",metadata,Column("id",INTEGER,primary_key=True),Column("name",String(20)))



engine = create_engine("mysql+pymysql://root:[email protected]:3306/s15_day13",max_overflow=5)

# metadata.create_all(engine)
engine.execute("INSERT INTO user (name) VALUES ('zhangsan')")

engine.execute("INSERT INTO user(name)VALUES (%s)",(("laoliu",),('laoqi',)))

# metadata.clear()
# metadata/remove(color)
conn =engine.connect()
conn.close()
 
   
例子2:
import alembic
import pymysql
from sqlalchemy import create_engine,Table,Column,MetaData,INTEGER,String,ForeignKey
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

engine =create_engine("mysql+pymysql://root:[email protected]:3306/s15_day13",max_overflow=5)

base =declarative_base()

class User(base):
    __tablename__ = "user"
    id = Column(INTEGER,primary_key=True)
    name = Column(String(20))

base.metadata.create_all(engine)


Session = sessionmaker(bind=engine)
session = Session()

u = User(id=5, name='sb')
session.add(u)
session.add_all([
    User(id=6, name='sb'),
    User(id=9, name='sb')
])
session.commit()
例子3:
# _*_ coding:utf-8 _*_
import alembic
import pymysql
from sqlalchemy import  create_engine,Table,Column,INTEGER,String,ForeignKey,UniqueConstraint,Index
from sqlalchemy.ext.declarative import  declarative_base
from sqlalchemy.orm import sessionmaker,relationship

engine =create_engine("mysql+pymysql://root:[email protected]:3306/s15_day13",max_overflow=5)
base =declarative_base()

#创建表
class User(base):
    __tablename__ = "user"
    id = Column(INTEGER,primary_key=True)
    name = Column(String(20))
    extra = Column(String(20))

    __table_args__ =(
        UniqueConstraint('id', 'name', name='uix_id_name'),
        Index('ix_id_name', 'name', 'extra'),
    )

#一对多
class Favor(base):
    __tablename__ = "favor"
    nid = Column(INTEGER,primary_key=True)
    caption = Column(String(20))

class Person(base):
    __tablename__ = "person"
    nid = Column(INTEGER,primary_key=True)
    name = Column(String(20),index=True,nullable=True)
    favor_id = Column(INTEGER,ForeignKey("favor.nid"))


#多对多
class Group(base):
    __tablename__ = "group"
    id = Column(INTEGER, primary_key=True)
    name = Column(String(64), unique=True, nullable=False)
    port = Column(INTEGER, default=22)

class Server(base):
    __tablename__ = "server"
    id = Column(INTEGER, primary_key=True, autoincrement=True)
    hostname = Column(String(64), unique=True, nullable=False)

class ServertoGroup(base):
    __tablename__ = "servertogroup"
    nid = Column(INTEGER, primary_key=True, autoincrement=True)
    server_id = Column(INTEGER, ForeignKey('server.id'))
    group_id = Column(INTEGER, ForeignKey('group.id'))
    group = relationship("Group",backref="s2g")
    server = relationship("Server",backref="s2g")



def init_db():
    base.metadata.create_all(engine)

def drop_db():
    base.metadata.drop_all(engine)


if __name__ == '__main__':
    # init_db()
    # drop_db()
    Session = sessionmaker(bind=engine)
    session = Session()
    ##增加表数据
    # obj = User(name="alex",extra="sb")
    # session.add(obj)
    # session.add_all([
    #     User(name="alex2",extra="sb2"),
    #     User(name="alex3",extra="sb3")
    # ]
    # )
    ##删除表数据
    ret = session.query(User.id,User.name,User.extra).filter(User.id >1).all()
    # ret1 = session.query(User.name, User.extra).all()
    # for i in  ret:
    #     print(i)
    print(ret)

    session.commit()




2.paramiko模块学习




未完待续。。。

你可能感兴趣的:(IT技术,日志,python自动化之路)