Django模型层_原生查询

2021-09-14  本文已影响0人  吕保鑫
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)

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)


上一篇下一篇

猜你喜欢

热点阅读