django使用原生sql
django使用原生SQL的方法
-
使用extra:
models.Book.objects.filter(publisher__name='djngo').extra(where=['price>50']) models.Book.objects.filter(publisher__name='djngo', price__gt=90) models.Book.objects.extra(select={'count': 'select count(*) from hello_Book'}) -
使用raw:
Book.objects.raw('select * from hello_Book') # 返回模型实例 -
执行自定义SQL语言:
from django.db import connection cursor=connection.cursor() # 插入操作 cursor.execute("insert into djngo(name) values('django')") # 更新操作 cursor.execute("update djngo set name='abc' where name='666'") # 删除操作 cursor.execute("delete from django'") # 查询操作 cursor.execute("select * from djngo") raw=cursor.fetchone() # 返回结果行游标直读向前,读取一条 cursor.fetchall() # 读取所有

浙公网安备 33010602011771号