Django--数据库查询操作

一、数据库数据常用操作细节

 (1)插入数据

1. 通过视图向DB中增加数据

   1. Entry.objects.create(属性=值,属性=值)

       例如: Author.objects.create(name=’’,age=)

2. 创建一个实体对象,并通过save()方法完成增加数据

  obj= Entry(属性=值,属性=值)

  obj.save()

3. 通过字典创建实体对象,再调用save()

  dic = {

      name = ‘’,

      age = ‘’

}

      obj = Author(**dic)

      obj.save()

(2)查询操作

     所有的查询都要在Entry.objects的基础上完成

     1. 基本查询操作

      1)语法:all()

       用法:Entry.objects.all()  相当于select * from   #Django已经将效率达到最好

       返回:QuerySet查询结果集,字典形式

      2)语法:Entry.objects.all().values()

        查询所有记录的某一列的值

        例如:Author.objects.all().values()   #相当于 select name from Author      

     3)语法: Entry.objects.all().values_list(‘列1’ , ’列2’)

    例如:Author.objects.all().values_list(‘name’,’age’ )  # #相当于 select name,age from Author

     注意:一定是从查询结果集中进行筛选,即必须要有all()方法

     1.1版本直接用values(‘name’,’age’)  取多个列值

   

 4) 语法: Entry.objects.get()

      只能查找一条记录,也只能返回一条记录。如果查询返回多条记录则报错

      即传入的参数一定要使得查询的记录只有一条,一般可以用id值

      如果不确定可以用try , except语句来捕捉错误

例如:

Author.objects.get(id=4)

      

     5) 语法: Entry.objects.exclude()

      作用:对给定条件取反

       例如:房屋租赁系统,不想查哪个区的房源

       Entry.objects.exclude(id=3) 

相当于 :select * from … where not id = 3;

Entry.objects.exclude(id=3,age=35)

相当于 :select * from … where not (id = 3 and age=35);

   

6) 语法: Entry.objects.order_by(‘列名1’ , ’-列名2’)

  作用:对所有查询数据进行排序,可以多级排序(只需要多写几个列名就行)

  注意:默认是升序排序,需要降序的话,只需要在列名前加‘—’

   

7)语法:Entry.objects.filter()

  根据自定义条件查询结果集。条件可以是一个,也可以是多个,多个的话,条件用’,’逗号隔开,其内部是使用AND来进行条件连接的。

  1. 使用Entry的属性来作为filter()的条件

   例如:Author.objects.filter(id=1)  #相当于select * from author where id=1

  2. 使用Field Lookups (查询谓词,主要结合filter或者get一起用)

     1. __exact 等值判断(用得比较少)

     语法:Entry.objects.get(id__exact=num)

      例如:Author.objects.get(id__exact=11)

       相当于select * from author where id=11

     2.__contains 模糊查询

      例如:Entry.objects.get(headline__contains = ‘Lennon’)

       相当于 select … where headline like  %Lennon%;      

         3. __in 范围查询

           Entry.objects.get(id__in =[1,3,5])

           相当于 select  …. where  id  in [1,3,5]

         4.__gt  __lt   大于,小于查询

           Entry.objects.get(id__gt=3)

           相当于  select  … where  id>3

         5. __startwith  以…开始  (注意与__contains区分)

Entry.objects.get(headline__startwith = ‘Lennon’)

            相当于select … where headline like  Lennon%;

      3.子查询

         例如:需求:获取比贾乃亮年龄要大的人

inner = Author.objects.filter(name__exat=’贾乃亮’).values(‘age’)  #获取贾乃亮的年龄

authors = Author.objects.filter(age__gt = inner)

实际上是嵌套查询

操作:

获取结果集的内容,因为结果集是许多条记录(集合)组成的一个列表,所以可以用下标来索引每一条记录,再通过索引来获得具体的字段,例如:

       name = authors[0][1]   #第一天记录是贾乃亮相关的信息,信息下的第二个字段为姓名

二、 F()操作和Q()操作

1. F()操作

    在执行中获取某列的值,再进行增删改查。

     语法: F(‘列名’)

例如: SQL语句中 update  author set age=age+10

     Django中写法:Author.object.all().update(age=F(‘age’)+10)

注意:需要导包

 from  django.db.models  import F

   应用:一般用在累加操作

  

2. Q()操作

Author.objects.filter(id=1,age=35)  这种情况是id=1 and age=35,无法写or逻辑运算符

     Author.objects.filter(

Q(id_exact=1)|Q(age=35),name=’王’   #也可以用,逗号表示and

)

     实现or逻辑运算符的生成

and逻辑运算应用:登录系统的用户和密码验证

猜你喜欢

转载自www.cnblogs.com/thomson-fred/p/9785656.html