Python短小精悍的Orator查询构造器
查询构造器
介绍
这个数据库查询构造器,提供便利的接口可以创建和执行查询操作,可以在大多数数据库中使用。
查询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()
Python短小精悍的Orator查询构造器的更多相关文章
- Python版的数据库查询构造器、ORM及动态迁移数据表。
Orator Orator提供一个简单和方便的数据库数据处理库. 它的灵感来源于PHP的Laravel框架,借助其思想实现了python版的查询构造器和ORM. 这是完整的文档:http://orat ...
- Python短小精悍的Orator基本使用技巧
基本使用 配置 设置数据库配置参数,创建一个DatabaseManager实例. from orator import DatabaseManager config = { 'mysql': { 'd ...
- python操作oracle数据库-查询
python操作oracle数据库-查询 参照文档 http://www.oracle.com/technetwork/cn/articles/dsl/mastering-oracle-python- ...
- Python 代码实现模糊查询
Python 代码实现模糊查询 1.导语: 模糊匹配可以算是现代编辑器(如 Eclipse 等各种 IDE)的一个必备特性了,它所做的就是根据用户输入的部分内容,猜测用户想要的文件名,并提供一个推荐列 ...
- Laravel框架使用查询构造器实现CURD
一.什么是查询构造器? ①Laravel 查询构造器(query Builder)提供方便,流畅的接口,用来建立及执行数据库查找语法 ②使用PDO参数绑定,以保护应用程序免于SQL注入因此传入的参数不 ...
- CI数据库操作_查询构造器类
=================数据库操作======================1.数据库配置: config/database.php 用户名 密码 数据库 2 加载数据库类:$this-& ...
- 【JEECG技术文档】JEECG高级查询构造器使用说明
功能介绍 高级查询构造器支持主子表联合查询,查询出更精确的数据. 要使用高级查询构造器需要完成以下步骤: 1. 在高级查询管理配置主子表信息. 2. 配置完后在JSP页面DataGrid标签上添加 ...
- 基于SQL和PYTHON的数据库数据查询select语句
#xiaodeng#python3#基于SQL和PYTHON的数据库数据查询语句import pymysql #1.基本用法cur.execute("select * from biao&q ...
- Laravel查询构造器简介
数据表 CREATE TABLE IF NOT EXISTS students( `id` INT AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR(255) NO ...
随机推荐
- 【DP】最长不下降子序列问题(二分)
Description 给你一个长度为n的整数序列,按从左往右的顺序选择尽量多的数字并且满足这些数字不下降. Thinking 朴素dp算法:F[i]表示到第i位为止的最长不下降子序列长度 F[i]= ...
- 一、Perfect Squares 完全平方数
一原题 Given a positive integer n, find the least number of perfect square numbers (, , , , ...) which ...
- 使用vsftp搭建ftp服务
第一步:安装vsftp pam db4 yum install vsftpd pam* db4*-y ================================================= ...
- EntityFramework 学习 一 CRUD using Stored Procedure: 使用存储过程进行CRUD操作
我们先创建如下3个存储过程 1.Sp_InsertStudentInfo: CREATE PROCEDURE [dbo].[sp_InsertStudentInfo] -- Add the param ...
- AngularJs 相应回车事件
最近做项目,要用到AngularJs,之前也有用过一点点,但仅限于数据的绑定,这次项目要整个前端需要使用这个框架,可能是不熟悉的原因,感觉这代码搞起来非常的不便利,:现总结一个响应回车事件: < ...
- 多线程编程-pthread 未定义的引用
多线程编程时用到函数库 pthread.h ,但是该函数库不是linux默认的函数库,所以编译c文件时,需要在目标代码文件后加上 -lpthread参数. 1.未加上 -lpthread 编译时,报错 ...
- linux命令学习笔记(38):cal 命令
cal命令可以用来显示公历(阳历)日历.公历是现在国际通用的历法,又称格列历,通称阳历. “阳历”又名“太阳历”,系以地球绕行太阳一周为一年,为西方各国所通用,故又名“西历”. .命令格式: cal ...
- ffmpeg编码h264只包含I帧P帧的方法
ffmpeg使用avcodc_encode_video编码,默认产生的h264包含B帧,在安防行业很多地方是不需要用到B帧的. 1.基础知识充电 这就涉及到h264的各种profile格式了,参考 h ...
- LOJ2303 「NOI2017」蚯蚓排队
「NOI2017」蚯蚓排队 题目描述 蚯蚓幼儿园有$n$只蚯蚓.幼儿园园长神刀手为了管理方便,时常让这些蚯蚓们列队表演. 所有蚯蚓用从$1$到$n$的连续正整数编号.每只蚯蚓的长度可以用一个正整数表示 ...
- bzoj 3091: 城市旅行 LCT
题目: http://www.lydsy.com/JudgeOnline/problem.php?id=3091 题解: 首先前三个操作就是裸的LCT模板 只考虑第四个操作. 要求我们计算期望,所以我 ...