django使用原生SQL语句查询两种方式
raw查询
#实例:查询租房项目的经理人信息
Agent.objects.raw('sql语句') --返回一个对象,但是还没有绕过orm框架
使用django中的connections来写sql语句
from django.db import connections
with connections['default'].cursor() as cursor:
cursor.execute("UPDATE TbEmp SET sal=sal+10 WHERE dno=30")
cursor.execute("SELECT ename, job FROM TbEmp WHERE dno=10")
row = cursor.fetchall()
#使用这个方法,[]中括号中参数使用哪个数据库,default默认
#拿游标对象
#拿数据方法 fetchall()全部数据 fetchone()取一条数据 fetchmany(n) n是想取多少条数据