1

I have a table "control" like this in local mysql (IP = 192.168.137.165)

Card_ID     Time_in      Time_out     Index
  101         7:00         11:00       0
  102         7:15         11:00       1
  103         6:45         12:00       1

Local and remote tables "control" are the same table

How to select all rows with index = 1 and insert to remote mysql using python? (IP=192.168.137.1)

I have ideas for backup database. If remote mysql alive, index = 0 and if remote mysql die, index = 1.

And if remote re-connect, I want to send data in local mysql to remote.So I want to just want to insert the line with index = 1

Code connect and insert mysql

import MySQLdb
db_local = MySQLdb.connect("localhost","root","loot","luan_van")
db = MySQLdb.connect("192.168.137.1","root_a","","luan_van")
def add_remote():
   with db:
       cur = db.cursor()
       cur.execute("INSERT INTO control(Card_ID,Time_in) VALUES ('%d', NOW())" %num)
       return

I can insert simple but I don't know insert multi-rows. Please help me. Thanks.

  • 3
    possible duplicate of [How to use python mysqldb to insert many values at once](http://stackoverflow.com/questions/14011160/how-to-use-python-mysqldb-to-insert-many-values-at-once) – Henrik Andersson Nov 09 '14 at 08:17
  • No.I have ideas for backup database. If remote mysql alive, index = 0 and if remote mysql die, index = 1. And if remote re-connect, I want to send data in local mysql to remote.So I want to just want to insert the line with index = 1 –  Nov 09 '14 at 08:39

1 Answers1

0

does this answer your question?

Inserting multiple rows in mysql

which will be something like this:

cur.execute("INSERT INTO control (Card_ID,Time_in) 
    VALUES('%d', NOW()),('%d', NOW()),('%d', NOW())", %num1, %num2, %num3)    
Community
  • 1
  • 1
FoosMaster
  • 65
  • 1
  • 11
  • No.I have ideas for backup database. If remote mysql alive, index = 0 and if remote mysql die, index = 1. And if remote re-connect, I want to send data in local mysql to remote.So I want to just want to insert the line with index = 1 –  Nov 09 '14 at 08:35