如何使用python将数据从本地备份到远程mysql?

时间:2014-11-10 10:00:40

标签: python python-2.7 python-3.x mysql-python

我有代码将数据添加到本地和远程(192.168.137.1)mysql。本地和远程具有相同的数据库(luan_van)和相同的表(tt_control),如此

Card_ID             Time_in           Time_out           Connect_remote
1001                 7:00              15:00                 1
1002                 8:00              19:00                 0
1003                 9:00              18:00                 0

这是我的子程序:

def connect_remote(): # this use to check connect alive
     try:
        db = MySQLdbconnect("192.168.137.1","root_a","","luan_van")
        return 1
     except MySQLdb.OperationalError, e:
        if e[0]=2003:
            return 0
 ##### this code add data to local mysql
db_local = MySQLdbconnect("localhost","root","root","luan_van")
def add_database_local():
     x = connect_remote()
     if(x==1):
         with db_local:
             cur = db_local.cursor()
             cur.execute("INSERT INTO tt_control(card_ID.Time_in,Ket_noi) VALUES ('%d',NOW(),1)" %num)
     else:
          with db_local:
             cur = db_local.cursor()
             cur.execute("INSERT INTO tt_control(card_ID.Time_in,Ket_noi) VALUES ('%d',NOW(),0)" %num) 
     return
 ###### this code add data to remote if it alive
def add_database_remote():
    x = connect_remote()
    if(x==1):
         db = MySQLdbconnect("192.168.137.1","root_a","","luan_van")
         with db:
             cur = db.cursor()
             cur.execute("INSERT INTO tt_control(card_ID.Time_in) VALUES ('%d',NOW())" %num)

我想检查connect remote alive。如果连接远程活动,我设置列connect_remote = 1,如果连接远程死,我在本地tt_control表中设置列connect_remote = 0。

如果远程死掉,我只将数据添加到本地mysql。现在如果远程重新连接我想要选择所有行都有connect_reomte = 0并将这些行发送到远程。

我该怎么办?请帮我。感谢。

0 个答案:

没有答案