查詢api
(1) all() :查詢所有結果 呼叫者:objects管理器 返回queryset
ret=book.objects.all()print(ret) #
, ,
(2) filter() : 它包含了與所給篩選條件相匹配的物件 呼叫者:objects管理器 返回queryset
#filter 方法:返回值queryset
ret=book.objects.filter(title="
linux
",price=111)
print(ret) #
]>
(3) get方法(): 返回與所給篩選條件相匹配的物件,返回結果有且只有一 呼叫者:objects管理器 返回查詢到model物件 (注意:查詢結果有且只有乙個才執行)
#get方法:返回查詢到model物件
ret=book.objects.get(price=111)
ret=book.objects.get(title="
linux")
print(ret.title) #
linux
(4) first(),last()方法: 返回第一條記錄和返回最後一條記錄 呼叫者:queryset 返回model物件
#first() last()方法:queryset呼叫 返回model物件
fbook=book.objects.all()[0]
fbook=book.objects.all().first()
lbook=book.objects.all().last()
(5) exclude(): 它包含了與所給篩選條件不匹配的物件 呼叫者:objects管理器 返回queryset
#exclude:返回值乙個queryset
ret=book.objects.exclude(price=111)
print(ret)
(6) order_by(): 對查詢結果排序 由queryset物件呼叫,返回值是queryset
#order_by:排序 由queryset物件呼叫,返回值是queryset
ret=book.objects.all().order_by("
-price
","-nid
").first()
print(ret)
(7) count : 數數 由queryset物件呼叫 返回int
#count :數數 :由queryset物件呼叫 返回int
ret=book.objects.all().count()
print(ret)
(8) reverse(): 對查詢結果反向排序 由queryset物件呼叫,返回值是queryset
#reverse():由queryset物件呼叫,返回值是queryset
book.objects.all().order_by("
price
").reverse()
(9) exists():如果queryset包含資料,就返回
true
,否則返回
false
由queryset物件呼叫 返回值布林值
#exists: 由queryset物件呼叫 返回值布林值
is_exist=book.objects.all().exists()
ifis_exist:
print("
ok")
(10)values()方法: 由queryset物件呼叫,返回值是queryset 乙個可迭代的字典序列
#values方法:由queryset物件呼叫,返回值是queryset
ret=book.objects.all().values("
title
","price
") #
queryset [,,...]
print(ret) #
'''ret=
for obj in book.objects.all():
temp=
'''
(11)values_list():由queryset物件呼叫,返回值是queryset 乙個元組序列
#values_list:由queryset物件呼叫,返回值是queryset
ret=book.objects.all().values_list("
title
","price")
print(ret) #
(12)distinct(): 從返回結果中剔除重複紀錄 由queryset物件呼叫,返回值是queryset
#distinct: 由queryset物件呼叫,返回值是queryset
ret=book.objects.all().values("
title
").distinct()
print(ret)
in是三者之中的任意乙個book.objects.filter(price__in=[100,200,300])
gt是大於
book.objects.filter(price__gt=100)
it是小於
book.objects.filter(price__lt=100)
range是在這個數之間
book.objects.filter(price__range=[100,200])
contains是只要包含在其中
book.objects.filter(title__contains="
python")
icontains是不區分大小寫只要包含在其中
book.objects.filter(title__icontains="
python")
startswith是以"py
"開頭的
book.objects.filter(title__startswith="py"
)時間book.objects.filter(pub_date__year=2012)
#查詢**大於200的書籍
ret=book.objects.filter(price__gte=200)
(ret)
#查詢書籍名稱以py開頭的所有的書籍名稱
ret=book.objects.filter(title__istartswith="
py").values("
title")
ret=book.objects.filter(title__contains="
p").values("
title")
print(ret) #
#查詢2023年7月份的所有的書籍
ret=book.objects.filter(pub_date__year=2017,pub_date__month=7)
print(ret)
查詢表記錄
基礎查詢 15.查詢emp中所有員工,顯示姓名 薪資 獎金 select name,sal,bonus from emp 16.查詢emp表中所有部門和職業 select dept,job from emp 17.查詢emp表中薪資大於3000的所有員工,顯示員工姓名 薪資 select name,...
python sqlite3查詢表記錄
desc 指 降序 解決的方法是 按照id 逆序排列,選取前10個 select from log info order by id desc limit 10 asc 指公升序 解決的方法是 按照id公升序排列,選取前10個 select from log info order by id asc...
表記錄的操作
一 插入語句 insert 1 向employee中插入三個員工資訊,要求員工姓名分別是zs,ls,wangwu 二 更新語句 update 1 將所有員工薪水修改為5000元。update employee set salary 5000 2 將姓名為 zs 的員工薪水修改為3000元。updat...