python数据库批量查询,批量保存
#-*-coding:utf-8-*-
from django.template import loader,Context,Template
from django.http import HttpResponse
from models import User
from django.shortcuts import render_to_response
def index(req):
#批量查询出数据
startId =1
endId=100
numId = (endId-startId)+1
numi=numId/20
addnumId= numId%20
for i in range(numi):
Idstart=startId
Idend=i*20
user =User.objects.filter(pk__gt=0).filter(pk__lt=5)
print user.values()
if len(user)>0:
print ‘fff‘
else:
print ‘kong‘
#print user
data1 =[]
data=[]
for key in user:
data1.append(key.name)
data1.append(key.age)
data1.append(key.sex)
# print data1
data.append(data1)
data1=[]
#批量保存
# for user in range data :
# user =user
#data=[[‘李四‘,12,‘女‘],[‘王五‘,56,‘男‘],[‘赵六‘,25,‘男‘]]
stu=User()
for user in data:
print user
#User.objects.create(name=user[0],age=user[1],sex=user[2])
#User.objects.create(user)
# user.save()
#print data
return render_to_response(‘index.html‘,{‘‘})
def instance(startId,endId):
#startId =1
#endId=100
numId= (endId-startId)+1
numi=numId/20
addnumId=numId%20
if addnumId >0:
numi+=1
for i in range(numi):
if i==0:
Idstart=startId
Idend =(i+1)*20+startId
print Idstart,Idend
elif i==(numi-1):
Idstart=startId+(i*20)
Idend =endId
print Idstart,Idend
else:
Idstart=startId+(i*20)
Idend =(i+1)*20+startId
print Idstart,Idend
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。