sqlalchemy ORM模块使用介绍

   前几天用到了flask框架,所以顺带介绍了flask-sqlalchemy扩展模块,去瞄一眼,由于有好多非web的python程序也用到了数据库,所以今天分享一下sqlalchemy的模块的使用方法.

Python版本:3.5

主要内容:

  1. 无关联表增删改查
  2. 一对多表增删改查
  3. 多对多表的增删改查

 

python使用不同mysql连接模块创建engine的方法:

 

  • 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>]

 

 

 

1.无关联关系的表

#info.py

#导入所需模块,类
from sqlalchemy import create_engine, Column, Integer, String, or_ #(create_engine:创建Engine实例的方法,Column:创建表字段类,Integer,String:字段类型)
from sqlalchemy.ext.declarative import declarative_base  #用于创建基类
from sqlalchemy.orm import sessionmaker   #(sessionmaker:创建与数据库的会类)

engine = create_engine('mysql+pymysql://root:weelin@10.10.33.229:3306/test?charset=utf8', echo=False) #创建一个映射到test(已经存在)数据库的连接,echo=True表示打印日志
Base = declarative_base()#生成orm基类

#建立映射关系
class Info(Base):
    __tablename__='info'
    id = Column(Integer,primary_key=True) #字段id,类型:整型,主键
    name = Column(String(30)) #字段name,类型:字符串,长度不大于30

    def __repr__(self):
        return "<User_obj:name='%s'>"%(self.name) #定义类的__repr__方法(类在外部表现形式)


Base.metadata.create_all(engine)  #根据上名定义的Info类创建表info,仅仅在初次生成数据表时有用,之后可屏蔽
Session_class = sessionmaker(bind=engine) #创建与数据库的会话session_class
Session = Session_class() #生成Session_class实例,用于操作数据库
#执行程序:python info.py,结果如下:
#很明显,表结构与我们定义完全一样
mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | info | +----------------+ 1 row in set (0.00 sec) mysql> show columns from info; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(30) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec)

无论增删改查都离不开筛选条件,下面以info为例介绍一下.

objs = Session.query(Info).all()  #objs等于Info表中的所有记录组成的列表

obj = Session.query(Info).first()    #obj等于Info表中第一条记录

obj = Session.query(Info).filter_by(id=1).first()  #obj等于满足id=1的第一个对象或者None

obj = Session.query(Info).filter(Info.id==1).first() #同上,注意“==”

#filter支持大于,小于,不等于,模糊匹配的操作,复杂筛选检建议用filter

obj = Session.query(Info).filter(Info.id!=9)[-1]      #obj为obj.id不等于9的最后一个对象

objs = Session.query(Info).filter(Info.id!=9).all()    #obj为obj.id不等于9的所有对象的列表

objs = Session.query(Info).filter(Info.id > 6).all()

 

#多条件(与)

obj = Session.query.filter_by(id=1,name="name1").first()   #obj等于满足id=1,name="name1"的第一条记录

obj = Session.query.filter(Info.id==1,Info.name=="name1").first()  #同上

 

obj = Session.query(Info).filter(Info.id!=9,Info.id!=1)[-1]      #obj为obj.id不等于9且不等与1的对象

obj = Session.query(Info).filter(Info.name != 9).filter(Info.id != 1)[-1]  #同上

 

#可选条件(或)使用or_

objs = Session.query(Info).filter(or_(Info.name == 'new',Info.id > 3)).all() #objs为对象name等于new或者id大于3的列表

#模糊匹配(类似正则用法)

"%"跟正则表达式中的*类似,表示0个或者多个"任意字符"

objs = Session.query(Info).filter(Info.name.like="new%").all()  #objs为Info.name以new开头的所有对象组成的列表(例如[<User_obj:name='newff'>, <User_obj:name='newname3'>, <User_obj:name='new'>])

objs = Session.query(Info).filter(Info.name.like="%new").all()  #objs为Info.name以new结尾的所有对象组成的列表(例如[<User_obj:name='new'>])

objs = Session.query(Info).filter(Info.name.like="%new%").all()  #objs为Info.name包含“new”的所有对象组成的列表(例如[[<User_obj:name='newff'>, <User_obj:name='newname3'>, <User_obj:name='ffnewff'>, <User_obj:name='new'>])

#排序--order_by

objs = Session.query(Info).order_by(Info.id).all()          #objs为以id升序后的所有对象组成的列表

objs = Session.query(Info).order_by(Info.id).all()          #objs为以id排序后的所有对象组成的列表

objs = Session.query(Info).order_by(desc(Info.id)).all()      #objs为以id降序后的所有对象组成的列表

#获取表的指定字段

print(Session.query(Info.name).order_by(desc(Info.id)).all()) #结果为:[('ffnewff',), ('newff',), ('new',), ('ffnewff',), ('newff',), ('newname3',), ('name1',)]

 #用列表指定查询范围,使用 "in_"

Session.query(Info.name,Info.id).filter(Info.id.in_([1,2,3])).all()  #结果为:[('name1', 1), ('newname3', 3)]

增、删、改、查操作

  •  增
#此处省去创建连接和表类的代码
#增 info1
= Info(name='name1') info2 = Info(name="name2") info3 = Info(name="name3") Session.add(info1) Session.add(info2) Session.add(info3) Session.commit()

#查看info表结果
mysql> select * from info;
+----+-------+
| id | name  |
+----+-------+
|  1 | name1 |
|  2 | name2 |
|  3 | name3 |
+----+-------+
3 rows in set (0.00 sec
#delete
objs = Session.query(Info).all()
for obj in objs:
    print (obj)
obj = Session.query(Info).filter_by(id=2).first()
Session.delete(obj)
Session.commit()
objs = Session.query(Info).all()
for obj in objs:
    print (obj)


#删除操纵前:
<User_obj:name='name1'>
<User_obj:name='name2'>
<User_obj:name='name3'>
#删除操作后:
<User_obj:name='name1'>
<User_obj:name='name3'>

#查看表info
mysql> select * from info;
+----+-------+
| id | name  |
+----+-------+
|  1 | name1 |
|  3 | name3 |
+----+-------+
2 rows in set (0.00 sec)
  •  
#
#obj = Session.query(Info).filter_by(id=3).first() obj = Session.query(Info).filter_by(id=3).first() obj.name='newname3' Session.commit()

#查看数据库
| id | name     |
+----+----------+
|  1 | name1    |
|  3 | newname3 |
+----+----------+
2 rows in set (0.00 sec)
  •  
#
obj = Session.query(Info).filter_by(name="newname3").first()
print(obj.id,obj.name,obj)
objs = Session.query(Info).all()
print (len(objs))

for obj in Session.query(Info).all():
    print (obj.name)


#输出
3 newname3 <User_obj:name='newname3'>
2
name1
newname3

 2.一对多(增、删、改、查部分参加上文,用法类似)

我们以经常用到的Book和Author模型来说明此问题,设定一个Author对象可以有多本书.

#Foreign_example.py

import sqlalchemy
from sqlalchemy import create_engine, Column, Integer, String,ForeignKey, or_,func,desc
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker,relationship,backref
engine = create_engine('mysql+pymysql://root:weelin@10.10.33.229:3306/test?charset=utf8', echo=False) #创建一个连接,echo=True表示打印日志
Base = declarative_base()#生成orm基类

class Author(Base):
    """一个作者多本书"""

    __tablename__ = 'author'
    id = Column(Integer,primary_key=True)
    name = Column(String(32))

    def __repr__(self):
        return "<Author_obj:name=%s, author_id=%s>"%(self.name, self.id)


class Book(Base):
    """一本书一名作者"""

    __tablename__='book'
    id = Column(Integer,primary_key=True)
    name = Column(String(100))
    author_id = Column(Integer,ForeignKey('author.id'))
    author = relationship('Author',backref = backref('books',order_by=id)) #这个字段不在表里体现

    def __repr__(self):
        return "<User_obj:name='%s',author_id=%s>"%(self.name,self.author_id)


Base.metadata.create_all(engine)
Session_class = sessionmaker(bind=engine)##创建与数据库的会话session class
Session = Session_class()#生成Session_class实例

 

执行python Foreign_example.py

 

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| author         | 
| book           | 
| info           | 
+----------------+
3 rows in set (0.00 sec)

mysql> show columns from author;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment | 
| name  | varchar(32) | YES  |     | NULL    |                | 
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> show columns from book;
+-----------+--------------+------+-----+---------+----------------+
| Field     | Type         | Null | Key | Default | Extra          |
+-----------+--------------+------+-----+---------+----------------+
| id        | int(11)      | NO   | PRI | NULL    | auto_increment | 
| name      | varchar(100) | YES  |     | NULL    |                | 
| author_id | int(11)      | YES  | MUL | NULL    |                | 
+-----------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

 

  • 给author添加数据
authors = [Author(name='沉川'),Author(name='怪灾'),Author(name='上燃'),Author(name='故乡月')]

try:
    for author in authors:
        Session.add(author)
    Session.commit()
except:
    print('rollback..')
    Session.rollback()

#查看数据库验证
mysql> select * from author;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | 沉川      |
|  2 | 怪灾      |
|  3 | 上燃      |
|  4 | 故乡月    |
+----+-----------+
4 rows in set (0.00 sec


#以下为对author表的操作
print(Session.query(Author).all())
#[<Author_obj:name=沉川, author_id=1>, <Author_obj:name=怪灾, author_id=2>, <Author_obj:name=上燃, author_id=3>, <Author_obj:name=故乡月, author_id=4>]

print(Session.query(Author).filter(Author.name == '沉川').all())
#[<Author_obj:name=沉川, author_id=1>]

print(Session.query(Author).filter(Author.name != '沉川').all())
#[<Author_obj:name=怪灾, author_id=2>, <Author_obj:name=上燃, author_id=3>, <Author_obj:name=故乡月, author_id=4>]

print(Session.query(Author).filter(Author.name == '沉川').first())
#<Author_obj:name=沉川, author_id=1>

print(Session.query(Author).filter(Author.name.like('故%')).all())
#[<Author_obj:name=故乡月, author_id=4>]

print(Session.query(Author).filter(Author.name.like('%月')).all())
#[<Author_obj:name=故乡月, author_id=4>]

print(Session.query(Author).filter_by(name = '沉川').all())
#[<Author_obj:name=沉川, author_id=1>]

print(Session.query(Author).filter(Author.name != '沉川',Author.id == 2).all())
#[<Author_obj:name=怪灾, author_id=2>]

print(Session.query(Author).filter(Author.name != '沉川').filter(Author.id == 2).all())
#[<Author_obj:name=怪灾, author_id=2>]

print(Session.query(Author).filter(Author.name != '沉川',Author.id > 2).all())
#[<Author_obj:name=上燃, author_id=3>, <Author_obj:name=故乡月, author_id=4>]


print(Session.query(Author).filter(or_(Author.name == '沉川',Author.id > 3)).all())
#[<Author_obj:name=沉川, author_id=1>, <Author_obj:name=故乡月, author_id=4>]
 
  • 给book添加数据
books = [Book(name='老成的北漂故事', author_id=1),
         Book(name='明代县令', author_id=1),
         Book(name='龙梦记', author_id=2),
         Book(name='孤剑玄刀诀1', author_id=3),
         Book(name='孤剑玄刀诀2', author_id=3),
         Book(name='孤剑玄刀诀3', author_id=4),
         Book(name='女友是个特警', author_id=4)
         ]
# #
try:
    for book in books:
        Session.add(book)
    Session.commit()
except:
    print('rollback..')
    Session.rollback()


#验证数据库
mysql> select * from book;
+----+-----------------------+-----------+
| id | name                  | author_id |
+----+-----------------------+-----------+
|  1 | 老成的北漂故事        |         1 |
|  2 | 明代县令              |         1 |
|  3 | 龙梦记                |         2 |
|  4 | 孤剑玄刀诀1            |         3 |
|  5 | 孤剑玄刀诀2            |         3 |
|  6 | 孤剑玄刀诀3            |         4 |
|  7 | 女友是个特警          |         4 |
+----+-----------------------+-----------+
7 rows in set (0.00 sec)

#以下为对book表的操作
print(Session.query(Book).first())
# <User_obj:name='老成的北漂故事',author_id=1>

book_obj = Session.query(Book).filter(Book.author_id == 1).first()
author_obj = Session.query(Author).filter(Author.name == '上燃').first()

print(book_obj,book_obj.name,book_obj.author_id,book_obj.author)
# <User_obj:name='老成的北漂故事',author_id=1> 老成的北漂故事 1 <Author_obj:name=沉川, author_id=1>

print(author_obj.name,author_obj.books)
# 上燃 [<User_obj:name='孤剑玄刀诀',author_id=3>, <User_obj:name='武林修罗',author_id=3>]

book_obj = Session.query(Book.name,Book.author_id).filter(Book.author_id == 1).first()
print(book_obj,book_obj[0])
# ('老成的北漂故事', 1) 老成的北漂故事

print(Session.query(Book).order_by(Book.id).all())
# [<User_obj:name='老成的北漂故事',author_id=1>, <User_obj:name='明代县令',author_id=1>, <User_obj:name='龙梦记',author_id=2>, <User_obj:name='孤剑玄刀诀',author_id=3>, <User_obj:name='武林修罗',author_id=3>, <User_obj:name='超能战士在校园',author_id=4>, <User_obj:name='女友是个特警',author_id=4>]

#print(Session.query(Book).order_by(desc(Book.id)).all())

print(Session.query(Book).filter(Book.id.in_([1,2,3])).all())
# [<User_obj:name='老成的北漂故事',author_id=1>, <User_obj:name='明代县令',author_id=1>, <User_obj:name='龙梦记',author_id=2>]

print(Session.query(func.count(Book.author_id),Book.author_id).group_by(Book.author_id).all())
# [(2, 1), (1, 2), (3, 3), (2, 4)]

print(Session.query(func.count(Book.name),Book.name).group_by(Book.name).all())
# [(1, '女友是个特警'), (1, '孤剑玄刀诀'), (1, '明代县令'), (1, '武林修罗'), (1, '老成的北漂故事'), (1, '超能战士在校园'), (1, '龙梦记')]
#通过Author的books属性创建Book对象

author_obj.books.append(Book(name='组织豪杰去抗日'))

Session.commit()
print(Session.query(Book.author_id,Book.name).filter(Author.name == '上燃').all())
#[(1, '老成的北漂故事'), (1, '明代县令'), (2, '龙梦记'), (3, '孤剑玄刀诀'), (3, '孤剑玄刀诀'), (4, '孤剑玄刀诀'), (4, '女友是个特警'), (3, '组织豪杰去抗日')]




查看数据库:
mysql> select * from book;
+----+-----------------------+-----------+
| id | name                  | author_id |
+----+-----------------------+-----------+
|  1 | 老成的北漂故事        |         1 |
|  2 | 明代县令              |         1 |
|  3 | 龙梦记                |         2 |
|  4 | 孤剑玄刀诀            |         3 |
|  5 | 孤剑玄刀诀            |         3 |
|  6 | 孤剑玄刀诀            |         4 |
|  7 | 女友是个特警          |         4 |
8 | 组织豪杰去抗日        |         3 | --------->新增一本author_id==3的书
+----+-----------------------+-----------+
8 rows in set (0.00 sec)

  2.多对多

 # many_to_many.py

import sqlalchemy
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey, Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import *

engine = create_engine('mysql+pymysql://root:weelin@10.10.33.229:3306/test?charset=utf8', echo=False) #创建一个连接,echo=True表示打印日志
Session_class = sessionmaker(bind=engine)  ##创建与数据库的会话session class
Session = Session_class()  # 生成Session_class实例
Base = declarative_base()  # 生成orm基类

student_m_to_m_teacher = Table('student_m_to_m_teacher', Base.metadata,
                               Column('student_id', Integer, ForeignKey('student.id')),
                               Column('teacher_id', Integer, ForeignKey('teacher.id'))
                               )

class Student(Base):
    __tablename__ = 'student'
    id = Column(Integer, primary_key=True)
    name = Column(String(20))
    teachers = relationship('Teacher', secondary=student_m_to_m_teacher, backref='students')

    def __repr__(self):
        return "<Student_obj:name='%s'>" % (self.name)
class Teacher(Base):
    __tablename__ = 'teacher'  # 表名
    id = Column(Integer, primary_key=True)
    name = Column(String(20))

    def __repr__(self):
        return "<Teacher_obj:name='%s'>" % (self.name)
Base.metadata.create_all(engine)  # 创建表结构
Session_class = sessionmaker(bind=engine)
Session = Session_class()

#执行 python many_to_many.py

mysql> show tables;
+------------------------+
| Tables_in_test         |
+------------------------+
| author                 | 
| book                   | 
| info                   | 
| student                | 
| student_m_to_m_teacher | 
| teacher                | 
+------------------------+
6 rows in set (0.00 sec)

mysql> show columns from student;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment | 
| name  | varchar(20) | YES  |     | NULL    |                | 
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> show columns from teacher;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment | 
| name  | varchar(20) | YES  |     | NULL    |                | 
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> show columns from student_m_to_m_teacher;
+------------+---------+------+-----+---------+-------+
| Field      | Type    | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+-------+
| student_id | int(11) | YES  | MUL | NULL    |       | 
| teacher_id | int(11) | YES  | MUL | NULL    |       | 
+------------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)
  • 添加数据
t1, t2, t3 = [Teacher(name='teach1'), Teacher(name='teach2'), Teacher(name='teach3')]

s1, s2, s3 = [Student(name='stud1'), Student(name='stud2'), Student(name='stud3')]

s1.teachers = [t1, t2]
s2.teachers = [t1, t2, t3]
s3.teachers = [t2, t3]
Session.add_all([t1, t2, t3, s1, s2, s3])
Session.commit()

#查看执行结果
mysql> select * from student;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    801
Current database: test

+----+-------+
| id | name  |
+----+-------+
|  1 | stud1 |
|  2 | stud3 |
|  3 | stud2 |
+----+-------+
3 rows in set (0.01 sec)

mysql> select * from teacher;
+----+--------+
| id | name   |
+----+--------+
|  1 | teach1 |
|  2 | teach2 |
|  3 | teach3 |
+----+--------+
3 rows in set (0.00 sec)

mysql> select * from student_m_to_m_teacher;
+------------+------------+
| student_id | teacher_id |
+------------+------------+
|          2 |          2 |
|          2 |          3 |
|          3 |          1 |
|          3 |          2 |
|          3 |          3 |
|          1 |          1 |
|          1 |          2 |
+------------+------------+
7 rows in set (0.00 sec)


#删除student_id等于3的学生对应的一名老师
#代码如下:
student_obj = Session.query(Student).filter_by(name="stud2").first()
teacher_obj = Session.query(Teacher).filter_by(name="teach2").first()
student_obj.teachers.remove(teacher_obj)
Session.commit()

print(student_obj.teachers)
#输出:[<Teacher_obj:name='teach1'>, <Teacher_obj:name='teach3'>]


#查看执行结果
mysql> select * from student_m_to_m_teacher;
+------------+------------+
| student_id | teacher_id |
+------------+------------+
|          2 |          2 |
|          2 |          3 |
|          3 |          1 |
|          3 |          3 |
|          1 |          1 |
|          1 |          2 |
+------------+------------+
6 rows in set (0.00 sec)

#直接删除一个作者学生时,会把这个学生跟所有老师的关联关系数据也自动删除
Session.delete(student_obj)
Session.commit()
#查看执行结果
mysql> select * from student;
+----+-------+
| id | name  |
+----+-------+
|  1 | stud1 |
|  2 | stud3 |
+----+-------+
2 rows in set (0.00 sec)

mysql> select * from teacher;
+----+--------+
| id | name   |
+----+--------+
|  1 | teach1 |
|  2 | teach2 |
|  3 | teach3 |
+----+--------+
3 rows in set (0.00 sec)

mysql> select * from student_m_to_m_teacher;
+------------+------------+
| student_id | teacher_id |
+------------+------------+
|          2 |          2 |
|          2 |          3 |
|          1 |          1 |
|          1 |          2 |
+------------+------------+
4 rows in set (0.00 sec)

#同理,直接删除一个老师对象时,会把这个老师跟所有学生的关联关系数据也自动删除

Session.delete(teacher_obj)
Session.commit()
#查看执行结果
mysql> select * from student;
+----+-------+
| id | name  |
+----+-------+
|  1 | stud1 |
|  2 | stud3 |
+----+-------+
2 rows in set (0.00 sec)

mysql> select * from teacher;
+----+--------+
| id | name   |
+----+--------+
|  1 | teach1 |
|  3 | teach3 |
+----+--------+
2 rows in set (0.00 sec)

mysql> select * from student_m_to_m_teacher;
+------------+------------+
| student_id | teacher_id |
+------------+------------+
|          2 |          3 |
|          1 |          1 |
+------------+------------+
2 rows in set (0.00 sec)

#添加一个学生和老师后,并把他们联系起来
student = Student(name='studtest')
Session.add(student)
teacher = Teacher(name='techtest')
Session.add(teacher)
student.teachers.append(teacher)
Session.commit()

#查看执行结果

mysql> select * from student_m_to_m_teacher;
+------------+------------+
| student_id | teacher_id |
+------------+------------+
|          2 |          3 |
|          1 |          1 |
|          4 |          4 |
+------------+------------+
3 rows in set (0.00 sec)

mysql> select * from teacher;
+----+----------+
| id | name     |
+----+----------+
|  1 | teach1   |
|  4 | techtest |
|  3 | teach3   |
+----+----------+
3 rows in set (0.00 sec)

mysql> select * from student;
+----+----------+
| id | name     |
+----+----------+
|  1 | stud1    |
|  2 | stud3    |
|  4 | studtest |
+----+----------+

 

posted @ 2017-02-23 12:37  weelin_area  阅读(1341)  评论(0编辑  收藏  举报