数据库数据查询

2022-09-14 18:35:22 浏览数 (1)

一、查询集

概述:查询数据的集合

分类

  • 原始查询集 使用 类名.query 得到原始查询集,注意不用原始查询集作为数据提取
  • 数据查询集 是由通过过滤器方法过滤原始查询集或者其他数据查询集得到

二、过滤器

注意:可以使用链式语法结构

all() 得到所有的数据查询集

以列表的形式返回

代码语言:javascript复制
<span class="hljs-meta">@view.route('/all/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_all</span><span class="hljs-params">()</span>:</span>
    allData = User.query.all()
    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">'show_data.html'</span>,data = allData)

show_data.html 内容如下

代码语言:javascript复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户信息</title>
    <style>
        table{
            border-collapse: collapse; /*合并单元格*/
            margin: auto; /*居中*/
        }
        tr>td{
            width: 150px; /*宽度*/
            border:1px solid chartreuse; /*边框*/
            text-align: center; /*文字水平居中*/
        }
    </style>
</head>
<body>
    <table>
        <caption><h2>用户信息展示表</h2></caption>
        <tr>
            <td>ID</td>
            <td>用户名</td>
            <td>性别</td>
            <td>年龄</td>
            <td>简介</td>
        </tr>
        {% for user in userData %}
        <tr>
            <td>{{ user.id }}</td>
            <td>{{ user.username }}</td>
            <td>{{ user.sex }}</td>
            <td>{{ user.age }}</td>
            <td>{{ user.info }}</td>
        </tr>
        {% endfor %}
    </table>
</body>
</html>

filter() 默认查询所有

格式

  • filter(类名.属性名.运算符(值))
  • filter(类名.属性名 数学运算符 值)
代码语言:javascript复制
<span class="hljs-meta">@view.route('/filter/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_filter</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-comment">#以下为可迭代对象</span>
    allData = User.query.filter() <span class="hljs-comment"># 查询所有数据</span>
    allData = User.query.filter(User.usex == <span class="hljs-keyword">True</span>) <span class="hljs-comment"># 只查询性别为True</span>
    allData = User.query.filter(User.id == <span class="hljs-number">1</span>) <span class="hljs-comment"># 查询id为1的</span>
    allData = User.query.filter(User.id != <span class="hljs-number">1</span>) <span class="hljs-comment"># 查询id不为1的</span>
    allData = User.query.filter(User.id != <span class="hljs-number">8</span>, User.usex==<span class="hljs-keyword">True</span>) <span class="hljs-comment"># 查询多个条件 and</span>
    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">'show_data.html'</span>, data = allData)

filter_by() 只支持单条件查询

格式:filter_by(属性名=值[,属性名=值])

代码语言:javascript复制

@view.route(’/filter_by/’) def test_filterBy(): # 查询id为1的数据 allData = User.query.filter_by(id = 1) # 下面俩种写法为错误写法(不支持) allData = User.query.filter_by(id > 1) allData = User.query.filter_by(id < 1) # and 操作 allData = User.query.filter_by(usex=False, id=1) return render_template(‘show_data.html’, data = allData)

代码语言:javascript复制
-  offset(num)  偏移数据量

```python
@view.route('/offset/')
def test_offset():
    #从第六条数据开始取
    allData = User.query.offset(5)
    # allData = User.query.all().offset(5)#错误写法 列表没有offset属性
    return render_template('show_data.html',data = allData)
  • limit(num) 取出多少条数据
代码语言:javascript复制
<span class="hljs-meta">@view.route('/limit/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_limit</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-comment"># allData = User.query.limit(5)</span>
    <span class="hljs-comment"># allData = User.query.offset(3).limit(5)</span>
    allData = User.query.filter(User.id &gt; <span class="hljs-number">4</span>).offset(<span class="hljs-number">3</span>).limit(<span class="hljs-number">5</span>)
    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">'show_data.html'</span>, data = allData)
  • order_by(类名.属性名) 排序

说明

  • 默认按照指定的字段 升序
  • -类名.属性名 降序

示例

代码语言:javascript复制
<span class="hljs-meta">@view.route('/order_by/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_order_by</span><span class="hljs-params">()</span>:</span>
    allData = User.query.order_by(User.id)
    allData = User.query.order_by(-User.id).offset(<span class="hljs-number">1</span>).limit(<span class="hljs-number">10</span>) <span class="hljs-comment">#从第一条取出十条数据按照id降序 </span>
    allData = User.query.filter(User.id&gt;<span class="hljs-number">5</span>,User.usex==<span class="hljs-keyword">True</span>).order_by(User.id).offset(<span class="hljs-number">1</span>).limit(<span class="hljs-number">10</span>)
    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">'show_data.html'</span>, data = allData)

get(id的值) 根据id查询 如果查询不到结果 返回None

代码语言:javascript复制

@view.route(’/get/’) def test_get(): #查询id为20 的数据 data = User.query.get(20) #查询不到返回 None # data = User.query.get(21) #错误写法 #data = User.query.get(uusername=‘aaa’) print(data) return ‘get’

代码语言:javascript复制
-  get_or_404()  根据index(编号)获取数据,如果未查询到则报404错误

u = User.query.get_or_404(index)

代码语言:javascript复制
- first()  在查询集中取出第一条数据

```python
@view.route('/first/')
def test_first():
    data = User.query.filter().first()  # 等于                        User.query.get(1) 只获取第一条数据 
    # 取出id最大的一条数据
    data = User.query.order_by(-User.id).first()
    print(data)

first_or_404() 获取查询集中的第一个元素,如果没有获取到则报404错误

代码语言:javascript复制
u = User.query.first_or_404()

count() 统计函数

代码语言:javascript复制
test_count = User.query.filter(~(User.usex==True)).count()

三、运算符

contains 包含关系 大小写不敏感

代码语言:javascript复制
<span class="hljs-meta">@view.route('/contains/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_contains</span><span class="hljs-params">()</span>:</span>
    allData = User.query.filter(User.uusername.contains(<span class="hljs-string">'lucky'</span>))
    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">'show_data.html'</span>,data = allData)

like 模糊查询

代码语言:javascript复制
<span class="hljs-meta">@view.route('/like/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_like</span><span class="hljs-params">()</span>:</span>
    allData = User.query.filter(User.uusername.like(<span class="hljs-string">'%3%'</span>)) <span class="hljs-comment"># 包含3</span>
    allData = User.query.filter(User.uusername.like(<span class="hljs-string">'a%'</span>)) <span class="hljs-comment"># a作为开头</span>
    allData = User.query.filter(User.uusername.like(<span class="hljs-string">'%c'</span>)) <span class="hljs-comment"># c作为结尾</span>
    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">'show_data.html'</span>,data = allData)
  • startswith endswith 以…开头和结尾. 大小写不敏感
代码语言:javascript复制
<span class="hljs-meta">@view.route('/startend/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_startend</span><span class="hljs-params">()</span>:</span>
    allData = User.query.filter(User.uusername.endswith(<span class="hljs-string">'张'</span>)) <span class="hljs-comment"># 以张作为结尾</span>
    allData = User.query.`filter(User.uusername.startswith(<span class="hljs-string">'张'</span>)) <span class="hljs-comment"># 以张作为开头</span>
    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">'show_data.html'</span>,data = allData)
  • 比较运算符
  • __gt__ 大于
  • __ge__ 大于等于
  • __lt__ 小于
  • __le__ 小于等于
代码语言:javascript复制
<span class="hljs-meta">@view.route('/ysf/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_ysf</span><span class="hljs-params">()</span>:</span>
    allData = User.query.filter(User.id.__ge__(<span class="hljs-number">5</span>)) <span class="hljs-comment"># == User.id&gt;=5</span>
    allData = User.query.filter(User.id.__gt__(<span class="hljs-number">5</span>)) <span class="hljs-comment"># == User.id&gt;5</span>
    allData = User.query.filter(User.id.__lt__(<span class="hljs-number">5</span>)) <span class="hljs-comment"># == User.id&lt;5</span>
    allData = User.query.filter(User.id.__le__(<span class="hljs-number">5</span>)) <span class="hljs-comment"># == User.id&lt;=5</span>
    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">'show_data.html'</span>,data = allData)

in_ notin_ 是否包含/不包含 在范围内

代码语言:javascript复制
<span class="hljs-meta">@view.route('/in_/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_in</span><span class="hljs-params">()</span>:</span>
    allData = User.query.filter(User.id.in_([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>])) <span class="hljs-comment"># 在...</span>
    allData = User.query.filter(~User.id.in_([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>])) <span class="hljs-comment"># 不在...</span>
    allData = User.query.filter(User.id.notin_([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>])) <span class="hljs-comment"># 不在...</span>
    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">'show_data.html'</span>,data = allData)

is null 为空的

代码语言:javascript复制
<span class="hljs-meta">@view.route('/null/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_null</span><span class="hljs-params">()</span>:</span>
    allData = User.query.filter(User.uinfo.is_(<span class="hljs-keyword">None</span>))
    allData = User.query.filter(User.uinfo == <span class="hljs-keyword">None</span>)
    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">'show_data.html'</span>, data=allData)

is not不为空的数据

代码语言:javascript复制
<span class="hljs-meta">@view.route('/notnull/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_not_null</span><span class="hljs-params">()</span>:</span>
    allData = User.query.filter(User.uinfo.isnot(<span class="hljs-keyword">None</span>))
    allData = User.query.filter(User.uinfo!= <span class="hljs-keyword">None</span>)
    allData = User.query.filter(~User.uinfo.is_(<span class="hljs-keyword">None</span>))
    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">'show_data.html'</span>, data=allData)

四、逻辑查询

逻辑与

导入

from sqlalchemy import and_

示例

代码语言:javascript复制
<span class="hljs-meta">@view.route('/and/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_and</span><span class="hljs-params">()</span>:</span>
    allData = User.query.filter(User.usex==<span class="hljs-keyword">True</span>,User.id.__lt__(<span class="hljs-number">20</span>))
    allData = User.query.filter(and_(User.usex==<span class="hljs-keyword">True</span>,User.id.__lt__(<span class="hljs-number">20</span>)))
    allData = User.query.filter(User.usex==<span class="hljs-keyword">True</span>).filter(User.id.__lt__(<span class="hljs-number">20</span>))
    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">'show_data.html'</span>, data=allData)

逻辑或

导入

from sqlalchemy import or_

示例

代码语言:javascript复制
<span class="hljs-meta">@view.route('/or/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_or</span><span class="hljs-params">()</span>:</span>
    allData = User.query.filter(or_(User.usex==<span class="hljs-keyword">True</span>,User.id.__lt__(<span class="hljs-number">20</span>)))
    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">'show_data.html'</span>, data=allData)
  • 逻辑非

导入

from sqlalchemy import not_

示例

代码语言:javascript复制
<span class="hljs-meta">@view.route('/not/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_not</span><span class="hljs-params">()</span>:</span>
    allData = User.query.filter(not_(User.usex==<span class="hljs-keyword">True</span>)) <span class="hljs-comment">#not里只能存在一个参数</span>
    allData = User.query.filter(~(User.usex==<span class="hljs-keyword">True</span>))
    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">'show_data.html'</span>, data=allData)

0 人点赞