Django模型层_原生查询

2022-08-04 10:43:03 浏览数 (1)

代码语言:javascript复制
from myproject.models import *
from django.db import connection
cur = connection.cursor()
cur.execute("select * from myproject_student")
#3
cs = cur.fetchall()
print(cs)
#((1, 'zhangsan', 88, 1), (2, 'lisi', 99, 1), (3, 'wangwu', 66, 2))
for c in cs:
    print(c)
    
#(1, 'zhangsan', 88, 1)
#(2, 'lisi', 99, 1)
#(3, 'wangwu', 66, 2)

代码语言:javascript复制
def test(sql):
    from django.db import connection
    with connection.cursor() as c:
        c.execute(sql)
        for info in c.fetchall():
            print(info)
test('select * from myproject_student')
#(1, 'zhangsan', 88, 1)
#(2, 'lisi', 99, 1)
#(3, 'wangwu', 66, 2)

0 人点赞