版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33867131/article/details/81985545
from django.shortcuts import render
# 1、导入connection模块
from django.db import connection
def index(request):
# 2、引入cursor()方法
cursor = connection.cursor()
# 3、cursor.execute("在这里写sql语句")
#cursor.execute("insert into book(id, name, author) values(null, '三国', '罗贯中')")
cursor.execute("select id, name, author from book")
# rows = cursor.fetchone() 返回一条数据
rows = cursor.fetchall() 返回全部数据
# rows = cursor.fetchmany(2) 返回2条数据(3,4,5,6,7,...)
for row in rows:
print(row)
return render(request, 'index.html')