python+django讀取json資料,同步資料庫
阿新 • • 發佈:2019-02-15
讀取txt檔案中的json
#!/usr/bin/python
#coding=utf-8from django.core.management import setup_environ
#import sys
#sys.path.append('..')
from payment import settings
setup_environ(settings)
#------------------------------------
from datetime import datetime,date,timedelta
from payment.models import *
from django.db.models import Q,Avg,Sum,Count
from django.db import connection
from django.db import transaction
from django.contrib.sessions.models import Session
import json
import time
from decimal import *
from payhandler import mytest,plans
from payment.app import log
import os
import sys
import re
logger=log.get_logger('syncdb',__name__)
data={}
datalog={}
#把所有表物件放到一個元組中
tab=(account,account_detail,account_recharge,account_recharge_log,user_login_log,credit_class,credit_rule,
festival_datelist,excute_datelist,schedule_list,credit_exec,partner_credit,
partner_credit_detail,latefee_detail,account_status_log,messages,ignore_date_list,amount_level,
account_daily_summary,account_daily_summary_detail)#需要同步的物件
#把同步到資料庫的資訊寫入syncdb.txt
def writetxt(jdata):
f=open('syncdb.txt','w')
f.write(jdata)
f.close()
#把需要同步的資料儲存
def test4(data1):
for key,value in data1.items():
tabstr=key
id=value
for j in tab:
strcut= j._meta.db_table
if cmp(strcut,tabstr)==0:
r1 =j.objects.using('aa').order_by('id').filter(pk__gt=id)[:3]
ids = []
for rvalue in r1:
ids.append(rvalue.id)
if len(ids)>0:
for k in ids:
r=j.objects.using('aa').get(id=k)
r.save(using='default')
r2=j.objects.using('default').get(id=k)
print (u'資料庫表:%s,同步id為:%d'%(tabstr,k))
data1[tabstr]=k
else:
del(data1[tabstr])
datalog[tabstr]=id
print(u"資料庫表:%s資料同步結束,結束id為:%d"%(tabstr,id))
def linkdic(data1):
if len(data1)==0:
data=datalog
elif len(datalog)==0:
data=data1
else:
data=dict(data1.items()+datalog.items())
jsondata=json.dumps(data)
writetxt(jsondata)
def readjson():#讀取txt中需要同步的json資料
print (u"***********************同步資料庫開始***************************")
starttime=time.time()
with open('syncdb.txt','r') as f:
fdata=f.read()
data1 = json.loads(fdata)
if len(data1)==0:
pass
else:
try:
while len(data1)>0:
test4(data1)
linkdic(data1)
except:
pass
finally:
linkdic(data1)
endtime=time.time()
print("同步資料所需的時間:"+str(endtime-starttime)+"秒")
print (u"***********************同步資料庫結束***************************")
time.sleep(200)
while True:
readjson()
def test():
print account_detail
r=account_detail.objects.using('aa').get(id=2676)
r=account_detail.objects.using('aa').filter(id=0)
print len(r)
# r.save(using='default')
#print r
#r.save(using='default')
#print r.nickname
r1=account_detail.objects.get(id=2676)
print r1
if __name__=='__main__':
#test()
readjson()
pass