python 学习笔记十一 SQLALchemy ORM(进阶篇)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 学习笔记十一 SQLALchemy ORM(进阶篇)相关的知识,希望对你有一定的参考价值。
SqlAlchemy ORM
SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,简言之便是:将对象转换成SQL,然后使用数据API执行SQL并获取执行结果.
Dialect用于和数据API进行交流,根据配置文件的不同调用不同的数据库API,从而实现对数据库的操作,如:
mysql-Python mysql+mysqldb://<user>:<password>@<host>[:<port>]/<dbname> pymysql mysql+pymysql://<username>:<password>@<host>/<dbname>[?<options>] MySQL-Connector mysql+mysqlconnector://<user>:<password>@<host>[:<port>]/<dbname> cx_Oracle oracle+cx_oracle://user:[email protected]:port/dbname[?key=value&key=value...] 更多详见:http://docs.sqlalchemy.org/en/latest/dialects/index.html
步骤一:
使用 Engine/ConnectionPooling/Dialect 进行数据库操作,Engine使用ConnectionPooling连接数据库,然后再通过Dialect执行SQL语句。
from sqlalchemy import create_engine import pymysql engine = create_engine("mysql+pymysql://[email protected]:3306/test",max_overflow=5) engine.execute( "INSERT INTO hosts (hostname,ip_addr,port,group_id) VALUES (‘web4‘,‘4.4.4.4‘,22,3)" ) result = engine.execute("select * from hosts") result.fetchall()
步骤二:
使用 Schema Type/SQL Expression Language/Engine/ConnectionPooling/Dialect 进行数据库操作。Engine使用Schema Type创建一个特定的结构对象,之后通过SQL Expression Language将该对象转换成SQL语句,然后通过 ConnectionPooling 连接数据库,再然后通过 Dialect 执行SQL,并获取结果。
from sqlalchemy import create_engine, Table, Column, Integer, String, MetaData, ForeignKey, select import pymysql #生成metadata类 metadata = MetaData() #创建user表,继承metadata类 #Engine使用Schama Type创建一个特定的结构对象 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))) #通过ConnectionPooling 连接数据库 engine = create_engine("mysql+pymysql://[email protected]:3306/test", max_overflow=5,echo=True) #通过Dialect执行SQL #metadata.create_all(engine) #创建表结构
""""增删改查""""" conn = engine.connect() #conn.execute(user.insert(),{‘id‘:2,"name":"koka"}) #conn.close() #sql = user.insert().values(id=2, name=‘akok‘) #conn.execute(sql) #conn.close() #sql = user.delete().where(user.c.id >1) #conn.execute(sql) #conn.close() #sql = user.update().values(fullname=user.c.name) #sql = user.update().where(user.c.name == "koka").values(name="okak") #conn.execute(sql) #conn.close() #sql = select([user, ]) => selct * from user #sql = select([user.c.id, ])=> select id from user #sql = select([user.c.name,color.c.name]).where(user.c.id==color.c.id) => #SELECT user.name, color.name FROM user, color WHERE user.id = color.id #sql = select([user.c.name]).order_by(user.c.name) => #SELECT user.name FROM user ORDER BY user.name #sql = select([user, ]).group_by(user.c.name) => #SELECT user.id, user.name FROM user GROUP BY user.name #conn.execute(sql) #conn.close()
实例:
from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, String from sqlalchemy.orm import sessionmaker import pymysql Base = declarative_base() #生成一个SQLORM基类 engine = create_engine("mysql+pymysql://[email protected]:3306/test",echo=True)
class Host(Base): __tablename__ = "hosts" id = Column(Integer, primary_key=True, autoincrement=True) hostname = Column(String(64), unique=True, nullable=False) ip_addr = Column(String(128), unique=True, nullable=False) port = Column(Integer, default=22)
def __repr__(self): return "<Host(hostname=‘%s‘,ip_addr=‘%s‘)>" % (self.hostname,self.ip_addr) """
# 无法删除之前创建的hosts表, # 这可能是MySQL在InnoDB中设置了foreign key关联,造成无法更新或删除数据。 # 可以通过设置FOREIGN_KEY_CHECKS变量来避免这种情况。 # SET FOREIGN_KEY_CHECKS = 0;CREATE TABLE hosts ( id INTEGER NOT NULL AUTO_INCREMENT, hostname VARCHAR(64) NOT NULL, ip_addr VARCHAR(128) NOT NULL, port INTEGER, PRIMARY KEY (id), UNIQUE (hostname), UNIQUE (ip_addr) ) """ #Base.metadata.create_all(engine) #创建所有表结构 metadata.create_all(engine) if __name__ == "__main__": SessionCls = sessionmaker(bind=engine) session = SessionCls() #h1 = Host(hostname="localhost", ip_addr="127.0.0.1") #h2 = Host(hostname="mysql", ip_addr="1.1.1.1", port=3306) #h3 = Host(hostname="web", ip_addr="10.0.0.10", port=8080) #session.add(h1) #h2.hostname = "mysqldb" #只要没提交,此时修改也没问题 #session.add_all([h1, h2, h3]) #session.rollback() #session.commit() res = session.query(Host).filter(Host.hostname.in_(["localhost", "mysqldb"])).all() print(res) """ [<Host(hostname=‘localhost‘,ip_addr=‘127.0.0.1‘)>, <Host(hostname=‘mysqldb‘,ip_addr=‘1.1.1.1‘)>] """
更多内容详见:
http://www.jianshu.com/p/e6bba189fcbd
http://docs.sqlalchemy.org/en/latest/core/expression_api.html
注:SQLAlchemy无法修改表结构,如果需要可以使用SQLAlchemy开发者开源的另外一个软件Alembic来完成.
步骤三:
使用 ORM/Schema Type/SQL Expression Language/Engine/ConnectionPooling/Dialect 所有组件对数据进行操作。根据类创建对象,对象转换成SQL,执行SQL。
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, String from sqlalchemy.orm import sessionmaker from sqlalchemy import create_engine import pymysql engine = create_engine("mysql+pymysql://[email protected]:3306/test", max_overflow=5, echo=True) #显示sql语句创建过程 Base = declarative_base() #生成一个SQLORM基类
#创建表 class User(Base): __tablename__ = "users" id = Column(Integer, primary_key=True) name = Column(String(50)) def __repr__(self): return "<User(id=‘%s‘,name=‘%s‘)>" % (self.id,self.name) # 寻找Base的所有子类,按照子类的结构在数据库中生成对应的数据表信息 #Base.metadata.create_all(engine) if __name__ == "__main__": Session = sessionmaker(bind=engine) session = Session() #增 #sql1 = User(id=1,name="haha") #sql2 = User(id=2,name="hehe") #session.add(sql1) #session.add_all([sql1, sql2]) #session.commit() #改 #session.query(User).filter(User.id >1).update({"name":"xixi"}) #session.commit() #删 #session.query(User).filter(User.id == 1).delete() #session.commit() #查 r1 = session.query(User).filter_by(name="xixi").first() r2 = session.query(User).filter_by(name="xixi").all() r3 = session.query(User).filter(User.name.in_(["haha", "hehe", "xixi"])).all() r4 = session.query(User.name.label("name_label")).all() #SELECT users.name AS name_label FROM users r5 = session.query(User).order_by(User.id).all() #SELECT users.id AS users_id, users.name AS users_name FROM users ORDER BY users.id r6 = session.query(User).order_by(User.id)[1:2] #SELECT users.id AS users_id, users.name AS users_name FROM users ORDER BY users.id LIMIT 1,2 print(r1, r2, r3, r4, r5, r6) session.commit()
外键关联
- FOREIGN KEY 约束是大多数(但不是所有)的关系型数据库中可以链接到主键列,或者拥有UNIQUE约束的列。
- FOREIGN KEY 能够引用多重列主键,并且其自身拥有多重列,被称为“复合外键”(composite foreign key)。其也能够引用这些列的子集(subset)。
- FOREIGN KEY 列作为对于其引用的列或者行的变化的响应能够自动更新其自身,比如CASCADE引用操作,这些都是内置于关系型数据库的功能之一。
- FOREIGN KEY 能够引用其自身的表,这个就涉及到“自引用”(self-referential)的外键了。
from sqlalchemy import Table, Column, Integer, ForeignKey, create_engine, String from sqlalchemy.orm import relationship,sessionmaker from sqlalchemy.ext.declarative import declarative_base engine = create_engine("mysql+pymysql://[email protected]:3306/test", max_overflow=5) Base = declarative_base() class Host(Base): __tablename__ = ‘host1‘ id = Column(Integer,primary_key=True,autoincrement=True) hostname = Column(String(64),unique=True,nullable=False) ip_addr = Column(String(128),unique=True,nullable=False) port = Column(Integer,default=22) group_id = Column(Integer, ForeignKey("group1.id")) group = relationship("Group", backref="host") #group_list = relationship("Group",back_populates="host_list")
class Group(Base): __tablename__ = "group1" id = Column(Integer, primary_key=True) name = Column(String(64),unique=True, nullable=False) #host_id = Column(Integer, ForeignKey("hosts.id")) #host_list = relationship("Host",back_populates="group_list") #Base.metadata.create_all(engine) if __name__ == "__main__": SessionCls = sessionmaker(bind=engine) session = SessionCls()
#先建组
#g1 = Group(name="g1") #g2 = Group(name="g2") #g3 = Group(name="g3")
#再建主机 h1 = Host(hostname="localhost1", ip_addr="127.0.0.1",group_id="1") h2 = Host(hostname="web2", ip_addr="192.168.1.10",group_id="2") h3 = Host(hostname="agent2", ip_addr="192.168.1.20",group_id="3") session.add_all([g1, g2, g3]) #session.add_all([h1, h2, h3]) session.commit()
映射关系
更多内容详见:http://www.xker.com/page/e2015/04/179550.html
from sqlalchemy import Table, Column, Integer, ForeignKey, and_, or_, func, create_engine, String from sqlalchemy.orm import relationship, sessionmaker from sqlalchemy.ext.declarative import declarative_base import pymysql Base = declarative_base()
engine = create_engine("mysql+pymysql://[email protected]:3306/test", max_overflow=5, echo=True) Host2Group = Table("host_2_group",Base.metadata, Column("host_id", ForeignKey("host.id"),primary_key=True), Column("group_id", ForeignKey("group.id"),primary_key=True) ) class Host(Base): __tablename__ = "host" id = Column(Integer, primary_key=True, autoincrement=True) hostname = Column(String(64), unique=True, nullable=False) ip_addr = Column(String(128), unique=True, nullable=False) port = Column(Integer, default=22) #group_id = Column(Integer, ForeignKey("group.id")) groups = relationship("Group", secondary=Host2Group, backref="host_list") #表示在group表中可以通过host_list查看host表内容,在host表中可以通过group查看group表内容 #group = relationship("Group", backref="host_list") #一边映射即可 #group = relationship("Group", back_populates="host_list") #使用populates两边名称要一致 #用于显示select查询结果 def __repr__(self): return "<id=%s, hostame=%s, ip_addr=%s>" %(self.id, self.hostname, self.ip_addr) class Group(Base): __tablename__ = "group" id = Column(Integer, primary_key=True) name = Column(String(64),unique=True,nullable=False) #host_id = Column(Integer, ForeignKey("hosts.id"))#使用此将会变成一对一的关系,一个组一个主机 #host_list = relationship("Host", back_populates="group")#使用populates两边名称要一致 #hosts = relationship("Host") def __repr__(self): return "<id=%s, name=%s>" % (self.id, self.name) #Base.metadata.create_all(engine) #创建数据结构 if __name__ == "__main__": SessionCls = sessionmaker(bind=engine) session = SessionCls() #h1 = Host(hostname="localhost",ip_addr="127.0.0.1") #h2 = Host(hostname="roomsvr",ip_addr="10.10.10.10") #session.add(h1) #session.add_all([h1, h2]) #g1 = Group(name=‘g1‘) #g2 = Group(name=‘g2‘) #g3 = Group(name=‘g3‘) #g4 = Group(name=‘g4‘) #session.add_all([g1,g2,g3,g4]) #session.commit() #groups = session.query(Group).all() #g1 = session.query(Group).first() #g2 = session.query(Group).filter(Group.name=="g2").first() #h2 = session.query(Host).filter(Host.hostname=="localhost").first() #h2.groups = groups #将主机同主机组关联 #print("=====>",h2.groups) #print("=====g1>",g1.host_list) #print("=====g2>",g2.host_list) #h2.groups.pop()
join
几个Join的区别 http://stackoverflow.com/questions/38549/difference-between-inner-and-outer-joins
- left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录
- right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录
- inner join(等值连接) 只返回两个表中联结字段相等的
#join 接上例代码 objs = session.query(Host).join(Host.groups).group_by(Group.name).all()#取两个表的交集,按组名排序 #SELECT * FROM host INNER JOIN host_2_group ON host.id = host_2_group.host_id #INNER JOIN `group` ON `group`.id = host_2_group.group_id GROUP BY `group`.id
#count objs = session.query(Host, func.count(Host.hostname)).group_by(Host.id).all() #SELECT * count(host.hostname) FROM host GROUP BY host.id
objs = session.query(Host,func.count(Group.name)). join(Host.groups).group_by(Group.name).all() #SELECT host.id,host.hostname,host.ip_addr,host.port,count(`group`.name) as count_1 FROM host #INNER JOIN host_2_group ON host.id = host_2_group.host_id INNER JOIN `group` ON `group`.id = #host_2_group.group_id GROUP BY `group`.name #print(objs) session.commit()
更多ORM内容:
http://files.cnblogs.com/files/wupeiqi/sqlalchemy.pdf.zip
Python MySQL API
python3.5 通过 pymysql 连接管理mysql
1.插入数据
import pymysql conn = pymysql.connect("127.0.0.1",user=‘root‘,db=‘test‘) cur = conn.cursor() recount = cur.execute("insert into students(name,sex,age,tel) values(‘koka‘,‘man‘,18,‘10000‘)") recount2 = cur.execute("insert into students(name,sex,age,tel) values(‘akok‘,‘man‘,20,‘10010‘)")
li = [ ("hehe","man",11,"110"), ("xixi","falme",12,"112")]
recount3 = cur.executemany("insert into students(name,sex,age,tel) values(%s,%s,%s,%s)",li)
conn.commit()
cur.close()
conn.close()
print(recount, recount2, recount3)2.删除数据
import pymysql
conn = pymysql.connect("127.0.0.1",user=‘root‘,db=‘test‘)
cur = conn.cursor()
recount = cur.execute("delete from students where name=‘akok‘;")
conn.commit()
cur.close()
conn.close()
3.修改数据
import pymysql
conn = pymysql.connect("127.0.0.1",user=‘root‘,db=‘test‘)
cur = conn.cursor()
recount = cur.execute("update students set age=24 where name=‘alex‘;")
conn.commit()
cur.close()
conn.close()
4.查数据
import pymysql
conn = pymysql.connect("127.0.0.1",user=‘root‘,db=‘test‘)
cur = conn.cursor()
recount = cur.execute("update students set age=24 where name=‘alex‘;") recount = cur.execute("select * from students;") print(cur.fetchone()) #匹配一条 print(cur.fetchone()) #cur.scroll(-1,mode="relative") #回退一条 print(cur.fetchone()) print(cur.fetchone()) cur.scroll(0,mode=‘absolute‘)#重置 print(cur.fetchone()) print(cur.fetchone())
conn.commit()
cur.close()
conn.close()############################## fetchall ##############################
import pymysql
conn = pymysql.connect("127.0.0.1",user=‘root‘,db=‘test‘)
cur = conn.cursor()
recount = cur.execute("select * from user;") nret = cur.fetchall() #所有查询到的数据以元组返回 conn.commit() cur.close() conn.close() print(nret) for i in nret: print(i[0],i[1])