学习sqlalchemy其实就是学习 python中的一个ORM组件
下面可以先看看资料,如果不太理解可以看看本文,循序渐进的了解知识点。
根据前面一篇文章,继续了解sqlalchemy基础,查询相关操作。此文与创建表、添加数据文章相关联,同上述文章所述,先创建一个会话Session
engine=create_engine("mysql+pymysql://test:[email protected]/pythonstudy",
encoding='utf-8', echo=True)
Base=declarative_base() #生成orm基类
Session=sessionmaker(bind=engine) #这里的Session只是一个类, 而不是实例
session=Session() #创建Session类实例
Query对象使用所创建的 query()上方法 Session。此函数采用可变数量的参数,这些参数可以是类和类检测描述符的任意组合。下面,我们指出 Query哪个加载User实例。在迭代上下文中计算时,将User返回存在的对象列表
查询实例分析
1. session.query(User).all(),映射所有数据集合
our_users = session.query(User).all()
for user in our_users:print('name:'+user.get_name()+' pwd:'+user.get_pwd())
结果如下:
2019-09-17 19:53:21,414 INFO sqlalchemy.engine.base.Engine SELECT user.id AS user_id, user.name AS user_name, user.password AS user_password
FROM user
2019-09-17 19:53:21,415 INFO sqlalchemy.engine.base.Engine {}
name:test pwd:hahha
name:wangfangchen pwd:123456
name:wendy pwd:foobar
name:mary pwd:xxg527
name:fred pwd:blah
name:fred pwd:safdsaewsa
2. session.query(User).order_by(User.id) 根据字段并排序
print('*'*30+'查询所有集合并打印,根据某个字段排序查找'+'*'*30)
for instance in session.query(User).order_by(User.id):
print(instance.id,instance.name, instance.password)
结果如下:
******************************查询所有集合并打印,根据某个字段排序查找******************************
2019-09-17 19:53:21,432 INFO sqlalchemy.engine.base.Engine SELECT user.id AS user_id, user.name AS user_name, user.password AS user_password
FROM user ORDER BY user.id
2019-09-17 19:53:21,432 INFO sqlalchemy.engine.base.Engine {}
1 test hahha
7 wangfangchen 123456
8 wendy foobar
9 mary xxg527
10 fred blah
11 fred safdsaewsa
3.查询指定字段,session.query(User.name, User.password):
print('*'*30+'查询指定字段并打印'+'*'*30)
for name,password in session.query(User.name, User.password):
print(name, password)
结果如下:
******************************查询指定字段并打印******************************
2019-09-17 19:53:21,449 INFO sqlalchemy.engine.base.Engine SELECT user.name AS user_name, user.password AS user_password
FROM user
2019-09-17 19:53:21,449 INFO sqlalchemy.engine.base.Engine {}
test hahha
wangfangchen 123456
wendy foobar
mary xxg527
fred blah
fred safdsaewsa
4.filter_by() 过滤查询,通过关键字
print('*'*30+'filter_by() 使用关键字参数:'+'*'*30)
for pwd, in session.query(User.password). filter_by(name='mary'):
print(pwd)
结果:
******************************filter_by() 使用关键字参数:******************************
2019-09-17 19:53:21,465 INFO sqlalchemy.engine.base.Engine SELECT user.password AS user_password
FROM user
WHERE user.name = %(name_1)s
2019-09-17 19:53:21,465 INFO sqlalchemy.engine.base.Engine {'name_1': 'mary'}
xxg527
5.filter_by接收的参数形式是关键字参数,而filter接收的参数是更加灵活的SQL表达式结构
for user in session.query(User).filter(User.name =='test').all():
print(user)
结果:
************************************************************
2019-09-17 19:53:21,495 INFO sqlalchemy.engine.base.Engine SELECT user.id AS user_id, user.name AS user_name, user.password AS user_password
FROM user
WHERE user.name = %(name_1)s
2019-09-17 19:53:21,495 INFO sqlalchemy.engine.base.Engine {'name_1': 'test'}
6.还有几个常用的过滤和查询关键词: equals、 not equal、like、in、notin、text、多filter|filter组合、count
print('*'*30+'常用的过滤操作equals'+'*'*30)
for user in session.query(User).filter(User.name =='test').all():
print(user)
print('*'*30+'常用的过滤操作not equal:'+'*'*30)
for user in session.query(User).filter(User.name !='test').all():
print(user)
print('*'*30+'常用的过滤操作like:'+'*'*30)
for user in session.query(User).filter(User.name.like('%ma%')).all():
print(user.get_name(),user.get_pwd())
print('*'*30+'常用的过滤操作 in 通配符:'+'*'*30)
for user in session.query(User).filter(User.name.in_(['wangfangchen','test'])).all():
print(user.get_name(),user.get_pwd())
print('*'*30+'常用的过滤操作 多次调用filter filter_by:'+'*'*30)
for user in session.query(User).filter(User.name =='test').filter(User.password=='hahha').all():
print(user.get_name(),user.get_pwd())
print('''
query.all(): all()返回列表
query.first(): 返回第一个元素
query.one(): 有且只有一个元素时才正确返回。
''')
print('*'*30+'filter函数还可以接收text对象,text是SQL查询语句的字面对象'+'*'*30)
for user in session.query(User).filter(text('id<8')).order_by(text('id')).all():
print(user.get_name(),user.get_pwd())
print('*'*30+'count'+'*'*30)
print('''
有两种count,第一种是纯粹是执行SQL语句后返回有多少行,对应的函数count(),
第二个是func.count(),适用在分组统计,比如按性别分组时,男的有多少,女的多少:
''')
number0=session.query(User).filter(User.name=='fred').count()
number1=session.query(func.count(), User.name).group_by(User.name).all()
print(number0)
print(number1)
结果如下:
************************************************************
2019-09-17 19:53:21,495 INFO sqlalchemy.engine.base.Engine SELECT user.id AS user_id, user.name AS user_name, user.password AS user_password
FROM user
WHERE user.name = %(name_1)s
2019-09-17 19:53:21,495 INFO sqlalchemy.engine.base.Engine {'name_1': 'test'}
******************************常用的过滤操作equals******************************
2019-09-17 19:53:21,511 INFO sqlalchemy.engine.base.Engine SELECT user.id AS user_id, user.name AS user_name, user.password AS user_password
FROM user
WHERE user.name = %(name_1)s
2019-09-17 19:53:21,511 INFO sqlalchemy.engine.base.Engine {'name_1': 'test'}
******************************常用的过滤操作not equal:******************************
2019-09-17 19:53:21,526 INFO sqlalchemy.engine.base.Engine SELECT user.id AS user_id, user.name AS user_name, user.password AS user_password
FROM user
WHERE user.name != %(name_1)s
2019-09-17 19:53:21,526 INFO sqlalchemy.engine.base.Engine {'name_1': 'test'}
******************************常用的过滤操作like:******************************
2019-09-17 19:53:21,542 INFO sqlalchemy.engine.base.Engine SELECT user.id AS user_id, user.name AS user_name, user.password AS user_password
FROM user
WHERE user.name LIKE %(name_1)s
2019-09-17 19:53:21,542 INFO sqlalchemy.engine.base.Engine {'name_1': '%ma%'}
mary xxg527
******************************常用的过滤操作 in 通配符:******************************
2019-09-17 19:53:21,557 INFO sqlalchemy.engine.base.Engine SELECT user.id AS user_id, user.name AS user_name, user.password AS user_password
FROM user
WHERE user.name IN (%(name_1)s, %(name_2)s)
2019-09-17 19:53:21,557 INFO sqlalchemy.engine.base.Engine {'name_1': 'wangfangchen', 'name_2': 'test'}
test hahha
wangfangchen 123456
******************************常用的过滤操作 多次调用filter filter_by:******************************
2019-09-17 19:53:21,572 INFO sqlalchemy.engine.base.Engine SELECT user.id AS user_id, user.name AS user_name, user.password AS user_password
FROM user
WHERE user.name = %(name_1)s AND user.password = %(password_1)s
2019-09-17 19:53:21,573 INFO sqlalchemy.engine.base.Engine {'name_1': 'test', 'password_1': 'hahha'}
test hahha
query.all(): all()返回列表
query.first(): 返回第一个元素
query.one(): 有且只有一个元素时才正确返回。
******************************filter函数还可以接收text对象,text是SQL查询语句的字面对象******************************
2019-09-17 19:53:21,588 INFO sqlalchemy.engine.base.Engine SELECT user.id AS user_id, user.name AS user_name, user.password AS user_password
FROM user
WHERE id<8 ORDER BY id
2019-09-17 19:53:21,588 INFO sqlalchemy.engine.base.Engine {}
test hahha
wangfangchen 123456
******************************count******************************
有两种count,第一种是纯粹是执行SQL语句后返回有多少行,对应的函数count(),
第二个是func.count(),适用在分组统计,比如按性别分组时,男的有多少,女的多少:
2019-09-17 19:53:21,604 INFO sqlalchemy.engine.base.Engine SELECT count(*) AS count_1
FROM (SELECT user.id AS user_id, user.name AS user_name, user.password AS user_password
FROM user
WHERE user.name = %(name_1)s) AS anon_1
2019-09-17 19:53:21,604 INFO sqlalchemy.engine.base.Engine {'name_1': 'fred'}
2019-09-17 19:53:21,619 INFO sqlalchemy.engine.base.Engine SELECT count(*) AS count_1, user.name AS user_name
FROM user GROUP BY user.name
2019-09-17 19:53:21,619 INFO sqlalchemy.engine.base.Engine {}
2
[(2, 'fred'), (1, 'mary'), (1, 'test'), (1, 'wangfangchen'), (1, 'wendy')]
此文源码地址