查询构造器

介绍

这个数据库查询构造器,提供便利的接口可以创建和执行查询操作,可以在大多数数据库中使用。

查询select操作

查询表中所有的数据。

users = db.table('users').get()

for user in users:
print(user['name'])

分片查询表中的数据

for users in db.table('users').chunk(100):
for user in users:
# ...

查询表中的某一条数据

user = db.table('users').where('name', 'John').first()
print(user['name'])

查询某一行的某一列数据

user = db.table('users').where('name', 'John').pluck('name')

查询某一列的数据

roles = db.table('roles').lists('title')

这个方法返回一个list,如果在加一个参数将返回一个字典。

roles = db.table('roles').lists('title', 'name')

指定一个selcet语句

users = db.table('users').select('name', 'email').get()

users = db.table('users').distinct().get()

users = db.table('users').select('name as user_name').get()

添加一个select语句在额外查询语句里。

query = db.table('users').select('name')

users = query.add_select('age').get()

使用where操作

users = db.table('users').where('age', '>', 25).get()

使用or操作

users = db.table('users').where('age', '>', 25).or_where('name', 'John').get()

使用where between操作

users = db.table('users').where_between('age', [25, 35]).get()

使用where not between操作

users = db.table('users').where_not_between('age', [25, 35]).get()

使用where in操作

users = db.table('users').where_in('id', [1, 2, 3]).get()

users = db.table('users').where_not_in('id', [1, 2, 3]).get()

使用where null查询Null记录

users = db.table('users').where_null('updated_at').get()

使用order by, group by and having操作

query = db.table('users').order_by('name', 'desc')
query = query.group_by('count')
query = query.having('count', '>', 100) users = query.get()

使用offset and limit操作

users = db.table('users').skip(10).take(5).get()

users = db.table('users').offset(10).limit(5).get()

使用Join操作

在查询构造器中也可以使用join连表查询。

基本join操作

db.table('users') \
.join('contacts', 'users.id', '=', 'contacts.user_id') \
.join('orders', 'users.id', '=', 'orders.user_id') \
.select('users.id', 'contacts.phone', 'orders.price') \
.get()

左连接操作left join

db.table('users').left_join('posts', 'users.id', '=', 'posts.user_id').get()

可以使用更高级的连表用法

clause = JoinClause('contacts').on('users.id', '=', 'contacts.user_id').or_on(...)

db.table('users').join(clause).get()

还可以加入where等条件

clause = JoinClause('contacts').on('users.id', '=', 'contacts.user_id').where('contacts.user_id', '>', 5)

db.table('users').join(clause).get()

where高级用法

有时候我们需要一些where更高级的用法,这些我们在orator中得以实现。

参数分组

db.table('users') \
.where('name', '=', 'John') \
.or_where(
db.query().where('votes', '>', 100).where('title', '!=', 'admin')
).get()

这个查询的sql将是这样。

SELECT * FROM users WHERE name = 'John' OR (votes > 100 AND title != 'Admin')

存在查询

db.table('users').where_exists(
db.table('orders').select(db.raw(1)).where_raw('order.user_id = users.id')
)

这个查询的sql将是这样。

SELECT * FROM users
WHERE EXISTS (
SELECT 1 FROM orders WHERE orders.user_id = users.id
)

聚合查询

这个查询构造器提供了聚合查询。例如:count, max, min, avg, sum

users = db.table('users').count()

price = db.table('orders').max('price')

price = db.table('orders').min('price')

price = db.table('orders').avg('price')

total = db.table('users').sum('votes')

原生表达式

有时候我们会用到原生表达式,但是要小心sql注入。我们可以使用raw方法,创建一个原生表达式。

db.table('users') \
.select(db.raw('count(*) as user_count, status')) \
.where('status', '!=', 1) \
.group_by('status') \
.get()

插入数据insert

往表中插入数据。

db.table('users').insert(email='foo@bar.com', votes=0)

db.table('users').insert({
'email': 'foo@bar.com',
'votes': 0
})

插入一条记录,并获取自增id。

db.table('users').insert([
{'email': 'foo@bar.com', 'votes': 0},
{'email': 'bar@baz.com', 'votes': 0}
])

更新数据update

更新表中数据。

db.table('users').where('id', 1).update(votes=1)

db.table('users').where('id', 1).update({'votes': 1})

增加或减少某一列的值。

db.table('users').increment('votes')  # Increment the value by 1

db.table('users').increment('votes', 5)  # Increment the value by 5

db.table('users').decrement('votes')  # Decrement the value by 1

db.table('users').decrement('votes', 5)  # Decrement the value by 5

指定增加某一行记录的值。

db.table('users').increment('votes', 1, name='John')

删除数据delete

删除数据。

db.table('users').where('age', '<', 25).delete()

删除所有的数据。

db.table('users').delete()

清空表数据

db.table('users').truncate()

合并unions

这个查询构造器提供了合并两个查询的方法。

first = db.table('users').where_null('first_name')

users = db.table('users').where_null('last_name').union(first).get()

注:使用union_all也可以。

悲观锁

这个查询构造器有一些方法,来帮助在查询中我们实现悲观共享锁。

db.table('users').where('votes', '>', 100).shared_lock().get()
db.table('users').where('votes', '>', 100).lock_for_update().get()

最新文章

  1. codevs 3369 膜拜
  2. C#文件和文件夹输入输出流代码
  3. Unity Adam特性整理
  4. 2014年2月份第4周51Aspx源码发布详情
  5. SpringMVC 实现邮件发送功能
  6. 重要常用的Lunix命令
  7. 前端 HTML基础
  8. FCKeditor 插件开发 示例
  9. SQL语言整理归纳
  10. jQuery Ajax异步处理Json数据详解
  11. springMVC注解优化
  12. 分享用于学习C++音频处理的代码示例
  13. Docker教程:docker远程repository和自建本地registry
  14. Oracle删除重复行
  15. Kali学习笔记40:SQL手工注入(2)
  16. ImportError: cannot import name descriptor_pb2
  17. BZOJ.5137.Standing Out from the Herd(广义后缀自动机)
  18. 机器学习入门 一、理解机器学习+简单感知机(JAVA实现)
  19. 大型运输行业实战_day08_1_memcache缓存生产应用
  20. Spring、springmvc配置

热门文章

  1. java网络爬虫爬虫小栗子
  2. case 练习
  3. bagging与boosting集成学习、随机森林
  4. EntityFramework 学习 一 DbContext
  5. php设计模式课程---6、策略模式如何使用
  6. HTTP-接触
  7. 分布式系统的Raft算法——在失联阶段这个老Leader的任何更新都不能算commit,都回滚,接受新的Leader的新的更新 意味着还是可能丢数据!!!
  8. [深入学习C#]C#实现多线程的方式:使用Parallel类
  9. HihoCoder 1636
  10. PPAS数据库备份与恢复