startId=int(ksId)-1
endId=int(jsId)
try:
r =account_detail.objects.using(‘aa‘).filter(pk__gt=startId).filter(pk__lt=endId)
#r =account_detail.objects.filter(pk__gt=startId).filter(pk__lt=endId)
#print r.values()
if len(r)>0:
for rvalue in r:
#print rvalue.objects.all()
rvalue.save()
#r.save(using=‘aa‘)
pass
print ("id在[%d--%d]间成功同步数据")%(startId+1,endId)
else:
print ("id在[%d--%d]间未查询到数据")%(startId+1,endId)
except:
print ("同步数据库id在%d-- %d段出现异常下次执行从%d开始")%(startId+1,endId,startId+1)
sys.exit()
print("同步数据库结束下次同步从 %d 开始")%endId
#print data[0].remark
#考虑到数据量有可能过大,分批同步,一批同步20行数据
def instance(startId,endId):
#startId =1
#endId=100
numId= (endId-startId)+1
numi=numId/20
if numi==0:
Idstart=startId
Idend=endId
test1(Idstart,Idend)
else:
addnumId=numId%20
if addnumId >0:
numi+=1
for i in range(numi):
if i==0:
Idstart=startId
Idend =(i+1)*20+startId
elif i==(numi-1):
Idstart=startId+(i*20)
Idend =endId
else:
Idstart=startId+(i*20)
Idend =(i+1)*20+startId
test1(Idstart,Idend)
#读取txt配置同步数据开始,结束id
def readtxt():
f =open("syncdb.txt")
line=f.readline()
n=[]
while line:
k= line[8:]
n.append(k)
line = f.readline()
f.close()
starttime=time.time()
instance(int(n[0]),int(n[1]))
endtime=time.time()
print ("***********************同步数据库结束***************************")
print("同步数据所需的时间:"+str(endtime-starttime)+"秒")
if __name__==‘__main__‘:
readtxt()
dango+python+mysql数据库同步(把数据库'aa'同步到default数据库中),布布扣,bubuko.com