import MySQLdb
import time
import datetime
# Create your tests here.
try:
now_time=time.strftime('%Y%m')
input_time = time.strftime('%Y-%m')
# reader data
readdb=MySQLdb.connect(host='192.168.0.206',db='cftest',user='cftest',passwd='123456!',port=3306)
rcur=readdb.cursor()
# conn.select_db('cftest')
uvpv=rcur.execute('select COUNT(*),COUNT(DISTINCT callerid) from core_callback_cdr_%s' %now_time)
wuvpv = []
rdata=rcur.fetchall()
for i in rdata:
wuvpv.append(i[0])
wuvpv.append(i[1])
wuv = wuvpv[1]
wpv = wuvpv[0]
print wuv,wpv
readdb.commit()
rcur.close()
readdb.close()
# writer data
writerdb = MySQLdb.connect(host='192.168.0.206',db='djtest',user='djtest',passwd='12345!',port=3306)
wcur = writerdb.cursor()
wcur.execute("insert into djweb_show_months(date,uv,pv) VALUE('%s','%s','%s') \
on DUPLICATE KEY UPDATE uv=VALUES(uv),pv=VALUES(pv)" % (input_time,wuv,wpv))
writerdb.commit()
wcur.close()
writerdb.close()
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
转载于:https://my.oschina.net/harlanblog/blog/535270