lxgi&

导航

两个mysql间同步表python脚步

#!/usr/bin/env python
import MySQLdb
import time
import sys

con1 = MySQLdb.connect(host='192.168.200.17', user='root', passwd='vnmkpwad', db='xoxdb',port=3306,charset='utf8')
con2 = MySQLdb.connect(host='localhost', user='root', passwd='vnmkpwad', db='xoxdb', port=3306,charset='utf8')
with con1:
cur = con1.cursor(MySQLdb.cursors.DictCursor)
cur.execute("SELECT * FROM tb_public_effect_tmp WHERE pm_isup = 0 ")
rows = cur.fetchall()
for row in rows:
pi = row["pm_index"]
ip = row["pm_ip"]
dk = row["pm_port"]
cond1 = [ pi, ip, dk ]

cur2 = con2.cursor(MySQLdb.cursors.DictCursor)
cur2.execute("SELECT * FROM tb_effect_info WHERE ei_pm_index = %s and ei_ip = %s and ei_port = %s", cond1)
numrows = int(cur2.rowcount)

if (not numrows):
ei_pm_index = pi
ei_type = row["pm_level"]
ei_tuijian = row["pm_auditstate"]
ei_price = row["pm_price"]

ei_filepath = row["pm_filepath"]
ei_make_name = row["pm_makername"]
ei_filename = row["pm_filename"]
ei_make_ikey = row["pm_makerid"]

ei_make_sn = row["pm_makersn"]
ei_file_msu = row["pm_filedesc"]
ei_file_fwei = row["pm_filepermit"]
ei_use_sn = row["pm_usersn"]

ei_test_time = row["pm_trytime"]
ei_up_time = int(time.time())
ei_filesize = row["pm_filelen"]
ei_ip = ip

ei_port = dk
ei_shensn = row["pm_auditsn"]
ei_shenmsu = row["pm_auditdesc"]
ei_shendatetime = row["pm_audittime"]

ei_isshen = '1'
ei_hash = row["pm_filedatemd5"]

ei_modelid = row["pm_modelid"]
ei_str1 = row["pm_str1"]

valArr = [ ei_pm_index, ei_type, ei_tuijian, ei_price, ei_filepath, ei_make_name, ei_filename, ei_make_ikey, ei_make_sn, ei_file_msu, ei_file_fwei, ei_use_sn, ei_test_time, ei_up_time, ei_filesize, ei_ip, ei_port, ei_shensn, ei_shenmsu, ei_shendatetime, ei_isshen, ei_hash, ei_modelid, ei_str1 ]
cur3 = con2.cursor()
cur3.execute("INSERT into tb_effect_info(ei_pm_index, ei_type, ei_tuijian, ei_price, ei_filepath, ei_make_name, ei_filename, ei_make_ikey, ei_make_sn, ei_file_msu, ei_file_fwei, ei_use_sn, ei_test_time, ei_up_time, ei_filesize, ei_ip, ei_port, ei_shensn, ei_shenmsu, ei_shendatetime, ei_isshen, ei_hash, ei_modelid, ei_str1) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", valArr)
cur3.execute("UPDATE tb_public_effect_tmp SET pm_isup = 1 WHERE pm_index = %s and pm_ip = %s and pm_port = %s", cond1)
con2.commit()

posted on 2015-11-26 19:23  lxgi&  阅读(427)  评论(0)    收藏  举报